-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printhex.c
56 lines (51 loc) · 1.47 KB
/
ft_printhex.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_printhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kristori <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/24 11:20:05 by kristori #+# #+# */
/* Updated: 2022/10/26 14:21:41 by kristori ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_hexlen(unsigned int nbr)
{
int i;
i = 0;
while (nbr != 0)
{
i++;
nbr = nbr / 16;
}
return (i);
}
static void ft_puthex(unsigned int nb, const char format)
{
if (nb >= 16)
{
ft_puthex(nb / 16, format);
ft_puthex(nb % 16, format);
}
else
{
if (nb <= 9)
ft_putchar(nb + '0');
else
{
if (format == 'x')
ft_putchar(nb - 10 + 'a');
if (format == 'X')
ft_putchar(nb - 10 + 'A');
}
}
}
int ft_printhex(unsigned int nb, const char format)
{
if (nb == 0)
return (ft_putchar('0'));
else
ft_puthex(nb, format);
return (ft_hexlen(nb));
}