-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base_unsigned.c
50 lines (45 loc) · 1.42 KB
/
ft_itoa_base_unsigned.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abassibe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/08 02:18:04 by abassibe #+# #+# */
/* Updated: 2017/04/13 14:14:27 by abassibe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int compte(unsigned long long nbr, int base)
{
int ret;
ret = 0;
while (nbr != 0)
{
nbr /= base;
ret++;
}
return (ret);
}
char *ft_itoa_base_unsigned(unsigned long long value, int base)
{
char *retour;
int nb;
int i;
char *tab;
tab = "0123456789abcdef";
if (value == 0)
return (ft_strdup("0"));
nb = compte(value, base);
retour = (char *)malloc(nb + 1);
i = 1;
while (value != 0)
{
retour[nb - i] = tab[value % base];
value /= base;
i++;
}
retour[nb] = '\0';
return (retour);
}