-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ulltoa.c
48 lines (43 loc) · 1.38 KB
/
ft_ulltoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ulltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vgroux <vgroux@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/24 13:52:03 by vgroux #+# #+# */
/* Updated: 2022/10/24 15:08:08 by vgroux ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_ulllen(unsigned long long n, char *base)
{
int len;
len = 0;
if (n == 0)
len++;
while (n > 0)
{
n /= ft_strlen(base);
len++;
}
return (len);
}
char *ft_ulltoa_base(unsigned long long n, char *base)
{
int i;
char *str;
i = ft_ulllen(n, base);
str = (char *)malloc(sizeof(char) * (i + 1));
if (!str)
return (NULL);
str[i--] = '\0';
if (n <= 0)
str[i] = 0;
while (i >= 0)
{
str[i--] = base[n % ft_strlen(base)];
n /= ft_strlen(base);
}
return (str);
}