-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf_hex.c
53 lines (48 loc) · 1.43 KB
/
printf_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mheinke <mheinke@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/03 10:52:48 by mheinke #+# #+# */
/* Updated: 2023/09/12 12:43:16 by mheinke ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int size_hex(unsigned int n)
{
int size;
if (n <= 0)
size = 1;
else
size = 0;
while (n)
{
size++;
n /= 16;
}
return (size);
}
int printf_hex(unsigned int n, int x_switch)
{
char *base_16;
int size;
size = size_hex(n);
base_16 = "0123456789abcdef";
if (x_switch == 0)
base_16 = "0123456789ABCDEF";
if (n < 16)
{
if (printf_char(base_16[n]) == -1)
return (-1);
}
else
{
if (printf_hex(n / 16, x_switch) == -1)
return (-1);
if (printf_hex(n % 16, x_switch) == -1)
return (-1);
}
return (size);
}