-
Notifications
You must be signed in to change notification settings - Fork 2
/
converter.c
52 lines (41 loc) · 847 Bytes
/
converter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "main.h"
/**
* convert - converts a number from one abse to another
* bases over 10 are not implemented yet. conversion to dest in progress
* @n: the number
* @b: the current base
* @d: the destination base
*
* Return: the number converted to its destination base
*/
void convert(int n, int b, int d)
{
int in_dec = convert_to_dec(n, b);
to_dest(n, in_dec, b, d);
}
int convert_to_dec(int n, int b)
{
int p, m = n, r = 0;
for (p = 0; m / 10 || m > 0; p++)
{
r = r + ((m % 10) * _pow(b, p));
m = m / 10;
}
return (r);
}
void to_dest(int n, int in_dec, int b, int d)
{
int r, p, e = in_dec;
if (d == 10)
{
printf("(%d)%d = (%d)%d\n", n, b, e, d);
return;
}
for (p = 0, r = 0; e / d || e > 0; p++)
{
r = r + ((e % d) * _pow(10, p));
e /= d;
}
printf("(%d)%d = (%d)%d\n", n, b, r, d);
return;
}