-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa_base.c
40 lines (37 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleonett <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/05 15:31:28 by mleonett #+# #+# */
/* Updated: 2018/04/13 12:33:56 by mleonett ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa_base(int nb, char *base)
{
int tmp;
int base_len;
int result_len;
char *result;
result_len = 0;
tmp = nb;
base_len = ft_strlen(base);
while (tmp)
{
result_len += 1;
tmp /= base_len;
}
if (!(result = (char *)malloc(sizeof(*result) * (result_len + 1))))
return (NULL);
result[result_len] = '\0';
while (nb)
{
result_len--;
result[result_len] = base[nb % base_len];
nb /= base_len;
}
return (result);
}