-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa_base.c
More file actions
executable file
·49 lines (45 loc) · 1.41 KB
/
ft_itoa_base.c
File metadata and controls
executable file
·49 lines (45 loc) · 1.41 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cmukwind <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/23 09:51:17 by cmukwind #+# #+# */
/* Updated: 2018/08/25 14:12:40 by cmukwind ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_pow(int nb, int pow)
{
if (pow == 0)
return (1);
else
return (nb * ft_pow(nb, pow - 1));
}
char *ft_itoa_base(int value, int base)
{
int i;
char *nbr;
int neg;
i = 1;
neg = 0;
if (value < 0)
{
if (base == 10)
neg = 1;
value *= -1;
}
while (ft_pow(base, i) - 1 < value)
i++;
nbr = (char*)malloc(sizeof(nbr) * i);
nbr[i + neg] = '\0';
while (i-- > 0)
{
nbr[i + neg] = (value % base) + (value % base > 9 ? 'A' - 10 : '0');
value = value / base;
}
if (neg)
nbr[0] = '-';
return (nbr);
}