-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itohex.c
49 lines (44 loc) · 1.41 KB
/
ft_itohex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itohex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbrandao <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/04 04:59:47 by dbrandao #+# #+# */
/* Updated: 2022/07/05 02:47:47 by dbrandao ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int hex_len(long unsigned int number)
{
int i;
if (!number)
return (1);
i = 0;
while (number)
{
number /= 16;
i++;
}
return (i);
}
char *ft_itohex(long unsigned int number)
{
char *hexnumber;
int remainder;
int len;
len = hex_len(number);
hexnumber = (char *) malloc(sizeof(char) * len + 1);
if (!hexnumber)
return (NULL);
hexnumber[len] = '\0';
remainder = 1;
while (len--)
{
remainder = number % 16;
number = number / 16;
hexnumber[len] = HEXMAP[remainder];
}
return (hexnumber);
}