-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_converthex.c
47 lines (42 loc) · 1.3 KB
/
ft_converthex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_converthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yrhiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 18:52:35 by yrhiba #+# #+# */
/* Updated: 2022/11/21 15:25:56 by yrhiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_hexlen(unsigned long n)
{
size_t i;
i = 0;
if (!n)
return (1);
while (n)
{
i++;
n /= 16UL;
}
return (i);
}
char *ft_converthex(unsigned long n, const char *base)
{
char *rtn;
size_t i;
i = ft_hexlen(n);
rtn = (char *)ft_calloc((i + 1), sizeof(char));
if (!rtn)
return (NULL);
while (n)
{
rtn[--i] = base[n % 16];
n /= 16UL;
}
if (!*rtn)
*rtn = '0';
return (rtn);
}