-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_hexadecimal.c
91 lines (83 loc) · 1.83 KB
/
ft_printf_hexadecimal.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_hexadecimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llatrice <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 14:50:50 by llatrice #+# #+# */
/* Updated: 2021/12/25 14:52:10 by llatrice ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_hex_x(unsigned long int n, int *i)
{
char *str;
int hex;
hex = 0;
str = "0123456789abcdef";
if (n < 0)
{
n = -n;
write (1, "-", 1);
(*i)++;
}
if (!n)
return ;
else
{
hex = n % 16;
ft_hex_x(n / 16, i);
}
write (1, &str[hex], 1);
(*i)++;
}
void ft_hex_xx(unsigned long int n, int *i)
{
char *str;
int hex;
hex = 0;
str = "0123456789ABCDEF";
if (n < 0)
{
n = -n;
write (1, "-", 1);
(*i)++;
}
if (!n)
return ;
else
{
hex = n % 16;
ft_hex_xx(n / 16, i);
}
write (1, &str[hex], 1);
(*i)++;
}
int ft_hex_pointer(unsigned long int n)
{
char *str;
int i;
str = "0123456789abcdef";
if (n < 16)
return (ft_singlechar(str[n]));
else
{
i = ft_hex_pointer(n / 16);
ft_hex_pointer(n % 16);
}
return (i + 1);
}
int ft_hex(unsigned int i_c, char f, int sum)
{
if (i_c)
{
if (f == 'x')
ft_hex_x(i_c, &sum);
else
ft_hex_xx(i_c, &sum);
}
else
ft_putchar('0', &sum);
return (sum);
}