-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_puthex.c
More file actions
56 lines (51 loc) · 1.48 KB
/
Copy pathft_puthex.c
File metadata and controls
56 lines (51 loc) · 1.48 KB
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_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: astoll <astoll@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 09:17:44 by astoll #+# #+# */
/* Updated: 2023/11/15 11:20:24 by astoll ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_hexlen(unsigned int n)
{
int count;
count = 0;
while (n != 0)
{
n /= 16;
count++;
}
return (count);
}
static void ft_puthexa(unsigned int n, const char format)
{
if (n >= 16)
{
ft_puthexa(n / 16, format);
ft_puthexa(n % 16, format);
}
else
{
if (n <= 9)
ft_putchar((n + '0'));
else
{
if (format == 'x')
ft_putchar((n - 10 + 'a'));
if (format == 'X')
ft_putchar((n - 10 + 'A'));
}
}
}
int ft_puthex(unsigned int n, const char format)
{
if (n == 0)
return (write(1, "0", 1));
else
ft_puthexa(n, format);
return (ft_hexlen(n));
}