-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_int_convs.c
109 lines (99 loc) · 2.08 KB
/
ft_printf_int_convs.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_int_convs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asoler <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/26 02:22:14 by asoler #+# #+# */
/* Updated: 2022/06/01 00:46:16 by asoler ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void reverse_write_digits(char *s, int count)
{
while (count)
{
count--;
write(1, &s[count], 1);
}
}
int ft_printf_int(int d)
{
char *n;
int n_len;
n = ft_itoa(d);
n_len = ft_strlen(n);
write(1, n, n_len);
free(n);
return (n_len - 1);
}
int ft_printf_address(unsigned long n)
{
char temp[20];
int i;
if (!n)
{
write(1, "(nil)", 5);
return (4);
}
i = 0;
while (n)
{
if (n % 16 > 9)
temp[i] = (n % 16) + 87;
else
temp[i] = (n % 16) + 48;
n /= 16;
i++;
}
temp[i] = '\0';
reverse_write_digits(temp, i);
return (ft_strlen(temp) + 1);
}
int ft_printf_usig_int(unsigned int n)
{
int i;
int r;
char digits[10];
i = 0;
while (n > 9)
{
digits[i] = (n % 10) + 48;
n /= 10;
i++;
}
digits[i] = n + 48;
r = i;
while (i >= 0)
{
write(1, &digits[i], 1);
i--;
}
return (r);
}
int ft_printf_int_as_hex(unsigned int n, char c)
{
char temp[9];
int i;
if (!n)
{
write(1, "0", 1);
return (0);
}
i = 0;
while (n)
{
if (n % 16 > 9)
temp[i] = (n % 16) + 87;
else
temp[i] = (n % 16) + 48;
n /= 16;
if (c == 'X')
temp[i] = ft_toupper(temp[i]);
i++;
}
temp[i] = '\0';
reverse_write_digits(temp, i);
return (ft_strlen(temp) - 1);
}