-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_hex.c
52 lines (46 loc) · 1.53 KB
/
ft_itoa_hex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/15 16:37:53 by jovicto2 #+# #+# */
/* Updated: 2023/07/15 16:41:29 by jovicto2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/libft.h"
static size_t ft_ilen(unsigned long long nbr, size_t base);
char *ft_itoa_hex(unsigned long long nbr, size_t base_len, const char *base)
{
char *string;
size_t length;
length = ft_ilen(nbr, base_len);
string = ft_calloc(length + 1, sizeof(char));
if (!string)
return (NULL);
if (!nbr)
{
*string = '0';
return (string);
}
while (length > 0)
{
string[--length] = base[nbr % base_len];
nbr /= base_len;
}
return (string);
}
static size_t ft_ilen(unsigned long long nbr, size_t base_len)
{
size_t counter;
counter = 0;
if (!nbr)
counter++;
while (nbr)
{
nbr /= base_len;
counter++;
}
return (counter);
}