-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa_base.c
61 lines (53 loc) · 1.73 KB
/
ft_itoa_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: npimenof <npimenof@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/09 13:57:42 by npimenof #+# #+# */
/* Updated: 2019/12/19 11:37:07 by npimenof ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char ft_get_base_char(unsigned long long n, int base)
{
char symbols[16];
ft_strncpy(symbols, "0123456789abcdef", base);
if (n > (unsigned long long)(base - 1))
return (symbols[n % base]);
return (symbols[n]);
}
static int ft_count_digits(unsigned long long n, int base)
{
int i;
if (n == 0)
return (1);
i = 0;
while (n > 0)
{
i++;
n /= base;
}
return (i);
}
static void ft_splitdigits_base(unsigned long long n, int b, int l, char *s)
{
if (n >= (unsigned long long)b)
{
ft_splitdigits_base(n / b, b, l - 1, s);
ft_splitdigits_base(n % b, b, l, s);
}
else
s[l] = ft_get_base_char(n, b);
}
char *ft_itoa_base(unsigned long long n, int base)
{
int len;
char *num;
len = ft_count_digits(n, base);
if (!(num = ft_strnew(len)))
return (NULL);
ft_splitdigits_base(n, base, len - 1, num);
return (num);
}