-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
56 lines (51 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jfreitas <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/21 16:36:04 by jfreitas #+# #+# */
/* Updated: 2020/02/16 21:09:55 by jfreitas ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char ft_calculate_char(int res, char c)
{
char ret_char;
ret_char = '0';
while (res)
{
ret_char++;
if (ret_char == ':')
ret_char = c;
res--;
}
return (ret_char);
}
char *ft_itoa_base(unsigned long n, int base, char c)
{
unsigned long nb;
int res;
int n_len;
char *str;
if (n == 0)
{
if (!(str = ft_strnew(1)))
return (NULL);
*str = '0';
return (str);
}
nb = n;
n_len = ft_intlen_base(n, base);
if (!(str = ft_strnew(n_len)))
return (NULL);
while (nb)
{
res = nb % base;
nb /= base;
str[n_len - 1] = ft_calculate_char(res, c);
n_len--;
}
return (str);
}