-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_hexa.c
66 lines (59 loc) · 1.59 KB
/
print_hexa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cyferrei <cyferrei@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 16:50:42 by cyferrei #+# #+# */
/* Updated: 2023/11/22 11:57:25 by cyferrei ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int hexa_len(unsigned int nb)
{
int len;
len = 0;
while (nb != 0)
{
len++;
nb = nb / 16;
}
return (len);
}
void conv_hex(unsigned int nb, const char form)
{
if (nb >= 16)
{
conv_hex(nb / 16, form);
conv_hex(nb % 16, form);
}
else
{
if (nb <= 9)
ft_putchar_fd((nb + '0'), 1);
else
{
if (form == 'x')
ft_putchar_fd((nb - 10 + 'a'), 1);
if (form == 'X')
ft_putchar_fd((nb - 10 + 'A'), 1);
}
}
}
int print_hexa(unsigned int nb, const char form)
{
if (nb == 0)
return (write(1, "0", 1));
else
conv_hex(nb, form);
return (hexa_len(nb));
}
/*int main(void)
{
unsigned int nb;
nb = -1685;
printf("%d\n", print_hexa(nb, 'x'));
printf("%x", nb);
return (0);
}*/