-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_uitohex.c
46 lines (41 loc) · 1.45 KB
/
ft_uitohex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_uitohex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ralves-b <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/28 17:25:42 by ralves-b #+# #+# */
/* Updated: 2022/07/04 22:09:56 by ralves-b ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t ft_count_hex_size(unsigned long int nb)
{
size_t counter;
counter = 1;
while (nb >= 16)
{
nb /= 16;
counter++;
}
return (counter);
}
char *ft_uitohex(unsigned long int nb)
{
const char *hexmap = "0123456789abcdef";
char *new_str;
size_t size_hex;
char *free_ret;
size_hex = ft_count_hex_size(nb);
new_str = (char *)malloc(sizeof(char) * (size_hex + 1));
new_str[size_hex] = '\0';
while (size_hex--)
{
new_str[size_hex] = hexmap[nb % 16];
nb /= 16;
}
free_ret = ft_strdup(new_str);
free(new_str);
return (free_ret);
}