-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putx.c
109 lines (99 loc) · 2.2 KB
/
ft_putx.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_putx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rd-agost <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/23 12:23:55 by rd-agost #+# #+# */
/* Updated: 2024/02/26 15:05:20 by rd-agost ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}
int ft_putstr(char *str)
{
int i;
i = 0;
if (str == NULL)
{
write(1, "(null)", 6);
return (6);
}
while (str[i])
{
write(1, &str[i], 1);
i++;
}
return (i);
}
int ft_putnbr(long long n)
{
long nb;
int c;
nb = n;
c = 0;
if (nb == -2147483648)
{
c += write(1, "-2147483648", 11);
return (11);
}
if (nb < 0)
{
c += write(1, "-", 1);
nb *= -1;
}
if (nb > 9)
{
c += ft_putnbr(nb / 10);
c += ft_putchar((nb % 10) + '0');
}
else
c += ft_putchar(nb + '0');
return (c);
}
int ft_puthex(unsigned long n, int is_uppercs)
{
char *lowcase;
char *upcase;
int len;
int temp;
lowcase = "0123456789abcdef";
upcase = "0123456789ABCDEF";
len = 0;
if (n == 0)
{
len += write(1, "0", 1);
return (len);
}
if (n > 0)
{
temp = n % 16;
if ((n / 16) > 0)
len += ft_puthex((n /= 16), is_uppercs);
if (is_uppercs == 0)
len += ft_putchar(lowcase[temp]);
else if (is_uppercs == 1)
len += ft_putchar(upcase[temp]);
}
return (len);
}
int ft_putptr(unsigned long memory_address)
{
unsigned long long temp;
int len;
len = 0;
if (!memory_address)
{
len += write(1, "(nil)", 5);
return (len);
}
temp = (unsigned long long)memory_address;
len += write(1, "0x", 2);
len += ft_puthex(temp, 0);
return (len);
}