-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ltoa_base.c
62 lines (56 loc) · 1.5 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: rabougue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/07/08 03:53:41 by rabougue #+# #+# */
/* Updated: 2016/08/06 08:51:08 by rabougue ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/libft.h"
static int ft_len(long nb, int base)
{
int len;
len = 0;
if (nb == 0)
return (1);
while (nb)
{
nb /= base;
len++;
}
return (len);
}
static char ft_char(long nb)
{
if (nb < 10)
return (nb + '0');
return (nb + 'a' - 10);
}
char *ft_ltoa_base(long value, int base)
{
long nb;
int neg;
char *str;
int len;
nb = value;
neg = 0;
if (nb < 0)
{
nb = -nb;
neg = 1;
}
len = ft_len(nb, base) + neg;
str = (char*)malloc(sizeof(char) * (len + 1));
str[len] = '\0';
while (--len >= 0)
{
str[len] = ft_char(nb % base);
nb /= base;
}
if (neg)
str[0] = '-';
return (str);
}