-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_hex.c
101 lines (97 loc) · 1.51 KB
/
print_hex.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
#include "holberton.h"
/**
* print_x - print in hexadecimal function
*@arg: integer to convert
*Return: printed number
*/
int print_x(va_list arg)
{
long int n = va_arg(arg, long int);
long unsigned int nb;
int tab[20];
int i, j, count = 0;
char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
int b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
if (n < 0)
{
_putchar('-');
count++;
nb = -n;
}
else
nb = n;
if (nb == 0)
{
_putchar('0');
count++;
return (count);
}
for (i = 0; nb != 0; i++)
{
tab[i] = nb % 16;
nb = nb / 16;
}
i--;
for (; i >= 0; i--)
{
for (j = 0; j < 17; j++)
{
if (tab[i] == b[j])
{
_putchar(a[j]);
count++;
break;
}
}
}
return (count);
}
/**
* print_X - print in hexadecimal function
*@arg: integer to convert
*Return: printed number
*/
int print_X(va_list arg)
{
long int n = va_arg(arg, long int);
long unsigned int nb;
int tab[20];
int i, j, count = 0;
char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
int b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
if (n < 0)
{
_putchar('-');
count++;
nb = -n;
}
else
nb = n;
if (nb == 0)
{
_putchar('0');
count++;
return (count);
}
for (i = 0; nb != 0; i++)
{
tab[i] = nb % 16;
nb = nb / 16;
}
i--;
for (; i >= 0; i--)
{
for (j = 0; j < 17; j++)
{
if (tab[i] == b[j])
{
_putchar(a[j]);
count++;
break;
}
}
}
return (count);
}