-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
49 lines (44 loc) · 1.39 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
43
44
45
46
47
48
49
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa_base.c :+: :+: */
/* +:+ */
/* By: rcappend <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/12/08 14:36:33 by rcappend #+# #+# */
/* Updated: 2021/11/03 14:23:37 by rcappend ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int counter(unsigned int n, int baselen)
{
int i;
i = 1;
while (n / baselen != 0)
{
i++;
n = n / baselen;
}
return (i);
}
char *ft_itoa_base(unsigned int n, char *base)
{
char *ret;
int len;
int baselen;
baselen = ft_strlen(base);
if (baselen == 0)
return (NULL);
len = counter(n, baselen);
ret = malloc(sizeof(unsigned char) * len + 1);
if (!ret)
return (NULL);
ret[len] = '\0';
while (len)
{
len--;
ret[len] = base[n % baselen];
n = n / baselen;
}
return (ret);
}