Repository navigation
Expand file tree
/
Copy pathft_printf_hex.c
More file actions
84 lines (77 loc) · 2.25 KB
/
Copy pathft_printf_hex.c
File metadata and controls
84 lines (77 loc) · 2.25 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: syokota <syokota@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/13 18:50:48 by syokota #+# #+# */
/* Updated: 2026/05/16 14:07:06 by syokota ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf_hex(unsigned int n, char convert)
{
int print_length;
char *base;
int error_check;
print_length = 0;
if (convert == 'x')
base = "0123456789abcdef";
else
base = "0123456789ABCDEF";
if (n >= 16)
{
error_check = ft_printf_hex(n / 16, convert);
if (error_check == -1)
return (-1);
print_length += error_check;
}
error_check = ft_printf_char(base[n % 16]);
if (error_check == -1)
return (-1);
print_length += error_check;
return (print_length);
}
static int ft_printf_hex_ptr(uintptr_t ptr)
{
int print_length;
char *base;
int error_check;
base = "0123456789abcdef";
print_length = 0;
if (ptr >= 16)
{
error_check = ft_printf_hex_ptr(ptr / 16);
if (error_check == -1)
return (-1);
print_length += error_check;
}
error_check = ft_printf_char(base[ptr % 16]);
if (error_check == -1)
return (-1);
print_length += error_check;
return (print_length);
}
int ft_printf_ptr(void *ptr)
{
int print_length;
int error_check;
print_length = 0;
if (ptr == NULL)
{
error_check = ft_printf_str("(nil)");
if (error_check == -1)
return (-1);
return (error_check);
}
error_check = ft_printf_str("0x");
if (error_check == -1)
return (-1);
print_length += error_check;
error_check = ft_printf_hex_ptr((uintptr_t)ptr);
if (error_check == -1)
return (-1);
print_length += error_check;
return (print_length);
}