-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_ltoa_base.c
62 lines (55 loc) · 1.61 KB
/
ft_ltoa_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
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaraval <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/02 18:07:31 by tmaraval #+# #+# */
/* Updated: 2019/10/03 12:51:43 by tmaraval ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_ltoa_cnt_digit(long nb, int base)
{
int cnt;
cnt = 0;
while (nb != 0)
{
nb = nb / base;
cnt++;
}
return (cnt);
}
void ft_ltoa_base_do(long nb, char *buffer, int base, int sign)
{
int i;
char charset[17];
int cnt;
cnt = ft_ltoa_cnt_digit(nb, base);
i = 1;
ft_strcpy(charset, "0123456789abcdef");
while (nb != 0)
{
buffer[cnt + sign - i] = charset[nb % base];
nb /= base;
i++;
}
if (sign && base == 10)
buffer[0] = '-';
buffer[cnt + sign] = '\0';
}
void ft_ltoa_base(long nb, char *buffer, int base)
{
int sign;
sign = (nb < 0);
if (sign == 1 && base == 10)
nb = nb * -1;
if (!nb)
{
ft_strncpy(buffer, "0\0", 2);
return ;
}
ft_ltoa_base_do(nb, buffer, base, sign);
return ;
}