-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_another_base.c
79 lines (72 loc) · 1.9 KB
/
convert_to_another_base.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* convert_to_another_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmiachko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/18 16:31:50 by vmiachko #+# #+# */
/* Updated: 2018/03/08 19:19:53 by vmiachko ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *change_base(size_t number, size_t base)
{
size_t i;
char *converted_number;
converted_number = ft_memalloc(40);
i = 0;
while (number)
{
converted_number[i] = number % base + 48;
number /= base;
++i;
}
converted_number[i] = '\0';
if (!ft_strlen(converted_number))
converted_number[0] = '0';
return (reverse_string(converted_number));
}
char *reverse_string(char *tmp)
{
char *tmp1;
int j;
int i;
j = (int)ft_strlen(tmp) - 1;
i = 0;
tmp1 = ft_memalloc((size_t)j + 2);
while (j > -1)
{
tmp1[i] = tmp[j];
--j;
++i;
}
free(tmp);
return (tmp1);
}
char *convert_to_hexidecimal(uintmax_t number, char c, int i)
{
char *tmp;
uintmax_t rem;
int pl;
tmp = "\0";
pl = 0;
if (c == 'x')
pl = 32;
if (number == 0)
{
tmp = ft_memalloc(2);
tmp[0] = '0';
}
while (number)
{
rem = number % 16;
tmp = ft_realloc(tmp, 2);
if (rem >= 10 && rem <= 16)
tmp[i++] = pl + 'A' + rem % 10;
else
tmp[i++] = rem + 48;
number /= 16;
}
return (reverse_string(tmp));
}