-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_puthex.c
85 lines (79 loc) · 1.38 KB
/
ft_puthex.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
#include "ft_printf.h"
//Print uppercase hexadecimals and returns the count.
int ft_puthex_upper(unsigned int nb, int fd)
{
int temp;
int i;
int hex_len;
char hex[100];
i = 0;
if (nb == 0)
return (ft_putchar('0', 1));
while (nb != 0)
{
temp = nb % 16;
if (temp < 10)
hex[i] = temp + 48;
else
hex[i] = temp + 55;
nb /= 16;
i++;
}
hex[i] = '\0';
hex_len = ft_strlen(hex);
while (hex_len > 0)
ft_putchar(hex[--hex_len], fd);
return (i);
}
//Print lowercase hexadecimals and also it can be used to print
//the address of a pointer. Returns the count.
int ft_puthex_lower(unsigned int nb, int fd)
{
int temp;
int i;
char hex[100];
int hex_len;
i = 0;
if (nb == 0)
return (ft_putchar('0', 1));
while (nb != 0)
{
temp = nb % 16;
if (temp < 10)
hex[i] = temp + 48;
else
hex[i] = temp + 87;
nb /= 16;
i++;
}
hex[i] = '\0';
hex_len = ft_strlen(hex);
while (hex_len > 0)
ft_putchar(hex[--hex_len], fd);
return (i);
}
int ft_puthex_addr(unsigned long int nb, int fd)
{
unsigned long int temp;
unsigned int i;
unsigned int hex_len;
char hex[100];
i = 0;
if (nb == 0)
return (ft_putchar('0', 1));
while (nb != 0)
{
temp = nb % 16;
if (temp < 10)
hex[i] = temp + 48;
else
hex[i] = temp + 87;
nb /= 16;
i++;
}
hex[i] = '\0';
hex_len = ft_strlen(hex);
while (hex_len > 0)
ft_putchar(hex[--hex_len], fd);
return (i);
}