-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path7-print_pointer.c
96 lines (94 loc) · 1.43 KB
/
7-print_pointer.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
#include "holberton.h"
/**
* comparehh - take arguments bigger than 10 and change it to char.
* @res: number given.
* Return: corresponding character.
*/
char comparehh(int res)
{
char h = 'a';
switch (res)
{
case 10:
h = 'a';
break;
case 11:
h = 'b';
break;
case 12:
h = 'c';
break;
case 13:
h = 'd';
break;
case 14:
h = 'e';
break;
case 15:
h = 'f';
break;
default:
h = res + '0';
break;
}
return (h);
}
/**
* imprimir - print nil when pointer NULL.
*
*/
void imprimir(void)
{
_putchar('(');
_putchar('n');
_putchar('i');
_putchar('l');
_putchar(')');
}
/**
* print_pointer - pointer address
* @va: arguments.
* Return: lenght of string.
*/
int print_pointer(va_list va)
{
char bin[64];
unsigned long int punt, t = 1, resp = 0;
int i = 0, j = 0, p = 0, cont = 0, flag = 0;
punt = va_arg(va, unsigned long int);
if (!punt)
{
imprimir();
return (5);
}
for (i = 0; i < 64; i++)
{
resp = ((t << (63 - i)) & punt);
resp = resp >> (63 - i);
bin[i] = (resp + '0');
}
for (p = 0; p <= 60 ; p += 4)
{
flag = 0;
for (j = 0; j < 4; j++)
{
if (bin[p + j] == '1')
flag = 1;
}
if (flag == 1)
break;
}
_putchar('0');
_putchar('x');
for (i = p; i <= 60; i += 4)
{
resp = 0;
resp += 8 * (bin[i + 0] - '0');
resp += 4 * (bin[i + 1] - '0');
resp += 2 * (bin[i + 2] - '0');
resp += 1 * (bin[i + 3] - '0');
_putchar(comparehh(resp));
cont++;
}
return (cont + 2);
}