-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
90 lines (79 loc) · 2.02 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pablomar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/13 18:39:16 by pablomar #+# #+# */
/* Updated: 2020/01/16 16:48:14 by pablomar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char ft_calculate_char(int mod, char c)
{
char return_char;
if (mod > 36)
return (0);
return_char = '0';
while (mod--)
{
return_char++;
if (return_char == ':')
return_char = c;
}
return (return_char);
}
static int ft_get_len(unsigned long num, unsigned long base)
{
int len;
len = 0;
while (num)
{
num /= base;
len++;
}
return (len);
}
char *ft_strnew(size_t size)
{
char *start;
start = (char *)malloc((size + 1) * sizeof(*start));
if (!start)
return (NULL);
ft_bzero(start, size + 1);
return (start);
}
static char *ft_generate_string(unsigned long num, unsigned long base, char c)
{
unsigned long sum;
int mod;
int len;
int i;
char *str;
if (num == 0)
{
if (!(str = ft_strnew(1)))
return (NULL);
*str = '0';
return (str);
}
sum = num;
i = 0;
len = ft_get_len(num, base);
if (!(str = ft_strnew(len)))
return (NULL);
while (sum)
{
mod = sum % base;
sum /= base;
str[(len--) - 1] = ft_calculate_char(mod, c);
}
return (str);
}
char *ft_itoa_base(unsigned long num, unsigned long base, char c)
{
char *str;
str = ft_generate_string(num, base, c);
return (str);
}