-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_hex.c
56 lines (51 loc) · 1.52 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aantonie <aantonie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 17:00:46 by aantonie #+# #+# */
/* Updated: 2024/02/12 12:10:02 by aantonie ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_hex_len(unsigned int num)
{
int length;
length = 1;
while (num / 16)
{
num = num / 16;
length++;
}
return (length);
}
void ft_put_hex(unsigned int num, const char format)
{
if (num >= 16)
{
ft_put_hex(num / 16, format);
ft_put_hex(num % 16, format);
}
else
{
if (num <= 9)
ft_putchar_fd((num + '0'), 1);
else
{
if (format == 'x')
ft_putchar_fd((num - 10 + 'a'), 1);
if (format == 'X')
ft_putchar_fd((num - 10 + 'A'), 1);
}
}
}
int ft_print_hex(unsigned int num, const char format)
{
if (num == 0)
return (write(1, "0", 1));
else
ft_put_hex(num, format);
return (ft_hex_len(num));
}