-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
62 lines (57 loc) · 1.61 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mavinici <mavinici@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/22 18:24:43 by mavinici #+# #+# */
/* Updated: 2021/05/22 18:24:43 by mavinici ### ########.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
static int count_digits(int num, int base)
{
int count;
long n;
n = num;
count = 1;
if (n < 0)
{
n *= -1;
count++;
}
while (n / base > 0)
{
n /= base;
count++;
}
return (count);
}
char *ft_itoa_base(int n, char *base, int len_base)
{
int i;
char *str;
int digits;
long num;
digits = count_digits(n, len_base);
num = n;
str = (char *)malloc(digits + 1);
if (str == NULL)
return (NULL);
str[digits] = '\0';
i = 0;
if (num < 0)
{
str[i++] = '-';
num *= -1;
}
while (digits > i && num / len_base > 0)
{
str[digits - 1] = base[num % len_base];
num /= len_base;
digits--;
}
str[i] = base[num % len_base];
return (str);
}