-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex_util.c
79 lines (70 loc) · 1.98 KB
/
hex_util.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hex_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibeliaie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/29 18:42:01 by ibeliaie #+# #+# */
/* Updated: 2023/05/30 12:51:53 by ibeliaie ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* count length of hexadecimal */
int ft_hexcount(unsigned long num)
{
int count;
count = 0;
if (num == 0)
return (1);
while (num > 0)
{
num /= 16;
count++;
}
return (count);
}
/* print int to lowercase hex conversion */
int ft_printlowx(unsigned long nbr, const char *hexbase)
{
int i;
i = 0;
if (nbr == 0)
i += ft_printchar('0');
if (nbr >= 16)
{
i += ft_printlowx(nbr / 16, "0123456789abcdef");
i += ft_printlowx(nbr % 16, "0123456789abcdef");
}
if (nbr < 16 && nbr != 0)
i += ft_printchar(hexbase[nbr]);
return (i);
}
/* print int to uppercase hex conversion */
int ft_printupx(unsigned long nbr, const char *hexbase)
{
int i;
i = 0;
if (nbr >= 16)
{
i += ft_printupx(nbr / 16, "0123456789ABCDEF");
i += ft_printupx(nbr % 16, "0123456789ABCDEF");
}
if (nbr < 16)
i += ft_printchar(hexbase[nbr]);
return (i);
}
/* print void pointer* to hex conversion */
int ft_printptr(void *ptr)
{
int count;
count = 0;
if (!ptr)
count += ft_printstr("0x0");
else
{
count += ft_printstr("0x");
count += ft_printupx(((unsigned long int)ptr), 0);
}
return (count);
}