-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
42 lines (39 loc) · 1.37 KB
/
ft_itoa_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tle-meur <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/22 15:03:59 by tle-meur #+# #+# */
/* Updated: 2015/12/22 15:04:01 by tle-meur ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa_base(int nb, int base)
{
int i;
int n;
char tmp[33];
static char basis[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
if (base < 2 || base > 36)
return (NULL);
if (!nb)
return (ft_strdup("0"));
i = 0;
if (!(ft_memset(tmp, 0, 33)))
return (NULL);
if ((n = nb) && nb < 0)
{
tmp[i++] = basis[-(n % base)];
n = -(n / base);
}
while (n)
{
tmp[i++] = basis[n % base];
n /= base;
}
if (nb < 0 && base == 10)
tmp[i] = '-';
return (ft_strrev(tmp));
}