-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ulltoa_base.c
49 lines (44 loc) · 1.42 KB
/
ft_ulltoa_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ulltoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jraymond <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/07 15:35:31 by jraymond #+# #+# */
/* Updated: 2018/02/14 16:46:11 by jraymond ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_ullcountint(unsigned long long nb, int base)
{
int len;
if (nb == 0)
return (1);
len = 0;
while (nb != 0)
{
nb /= base;
len++;
}
return (len++);
}
char *ft_ulltoa_base(unsigned long long nb, int base)
{
int len;
char *buf;
char rest[17];
ft_strcpy(rest, "0123456789abcdef");
len = ft_ullcountint(nb, base);
if (!(buf = (char*)malloc(sizeof(char) * (len + 1))))
return (NULL);
buf[len] = '\0';
len--;
while (len >= 0)
{
buf[len] = rest[ft_llabs((nb % base))];
len--;
nb /= base;
}
return (buf);
}