-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_utoa.c
51 lines (46 loc) · 1.41 KB
/
ft_utoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tairribe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 16:02:02 by tairribe #+# #+# */
/* Updated: 2022/11/21 16:03:05 by tairribe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_size(unsigned long n, int base)
{
int i;
i = 0;
while (n)
{
i++;
n = n / base;
}
return (i);
}
char *ft_utoa(unsigned long n, int base)
{
int re;
int size;
char *str_nb;
if (base < 2 || base > 16)
return (NULL);
if (n == 0)
return (ft_strdup("0"));
size = get_size(n, base);
str_nb = ft_calloc(size + 1, sizeof(char));
size--;
while (n)
{
re = n % base;
if (re > 9)
str_nb[size--] = re - 10 + 'a';
else
str_nb[size--] = re + '0';
n = n / base;
}
return (str_nb);
}