-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_hex.c
58 lines (52 loc) · 1.54 KB
/
print_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
53
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/12 17:08:46 by sde-silv #+# #+# */
/* Updated: 2023/07/12 17:08:49 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int hex_len(unsigned long int num)
{
int len;
len = 0;
if (num == 0)
return (1);
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}
static void ft_put_hex(unsigned long int num, int format)
{
if (num >= 16)
{
ft_put_hex(num / 16, format);
ft_put_hex(num % 16, format);
}
else
{
if (num < 10)
print_chr(num + '0');
else
{
if (format == 'x' || format == 'p')
print_chr(num - 10 + 'a');
else if (format == 'X')
print_chr(num - 10 + 'A');
}
}
}
int print_hex(unsigned long int next, int format)
{
unsigned long int val;
val = (unsigned long int)next;
ft_put_hex(val, format);
return (hex_len(val));
}