-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils_hex.c
More file actions
108 lines (99 loc) · 2.21 KB
/
ft_printf_utils_hex.c
File metadata and controls
108 lines (99 loc) · 2.21 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acosta-a <acosta-a@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/01 08:57:39 by acosta-a #+# #+# */
/* Updated: 2022/05/19 13:44:40 by acosta-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
unsigned long long ft_putptr(unsigned long long n, int fd, int type)
{
int len;
char c[19];
unsigned long long nb;
nb = n;
len = 0;
{
if (nb == 0)
return (ft_putstr_fd("(nil)", fd));
while (nb > 0)
{
c[len++] = nb % 16;
nb = nb / 16;
}
}
return (ft_putstr_ptr (c, fd, len, type));
}
int ft_putstr_ptr(char *s, int fd, int len, int type)
{
int i;
i = len - 1;
if (s == NULL)
{
write (fd, "(null)", 6);
return (6);
}
write(fd, "0x", 2);
len = len + 2;
while (i >= 0)
{
if (s[i] <= 9)
ft_putchar_fd(s[i--] + '0', fd);
else
{
if (type == 0)
ft_putchar_fd(s[i] - 10 + 'a', fd);
i--;
}
}
return (len);
}
int ft_puthex_len(unsigned int n, int fd, int type)
{
int len;
char c[10];
unsigned int nb;
nb = n;
len = 0;
{
if (nb == 0)
{
ft_putchar_fd('0', fd);
return (1);
}
while (nb > 0)
{
c[len++] = nb % 16;
nb = nb / 16;
}
}
return (ft_putstr_hex (c, fd, len, type));
}
int ft_putstr_hex(char *s, int fd, int len, int type)
{
int i;
i = len - 1;
if (s == NULL)
{
write (fd, "(null)", 6);
return (6);
}
while (i >= 0)
{
if (s[i] <= 9)
ft_putchar_fd(s[i--] + '0', fd);
else
{
if (type == 0)
ft_putchar_fd(s[i] - 10 + 'a', fd);
if (type == 1)
ft_putchar_fd(s[i] - 10 + 'A', fd);
i--;
}
}
return (len);
}