-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_int.c
More file actions
111 lines (104 loc) · 1.66 KB
/
Copy pathprintf_int.c
File metadata and controls
111 lines (104 loc) · 1.66 KB
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
110
111
#include "main.h"
/**
* print_u_int - Print an integer to the std output
* @n: interger to print
*
* Return: void
*/
int print_u_int(unsigned int n)
{
char buff[10], temp;
int i = 0, j;
do {
buff[i++] = '0' + n % 10;
n /= 10;
} while (n > 0);
buff[i] = '\0';
j = 0;
i--;
while (i > j)
{
temp = buff[j];
buff[j] = buff[i];
buff[i] = temp;
i--;
j++;
}
return (_writestring(buff));
}
/**
* print_int - Print an integer to the std output
* @n: interger to print
*
* Return: void
*/
int print_int(long int n)
{
char buff[10], temp;
int i = 0, sign = 1, j;
if (n < 0)
{
sign = -1;
n = -n;
}
do {
buff[i++] = '0' + n % 10;
n /= 10;
} while (n > 0);
if (sign < 0)
buff[i++] = '-';
buff[i] = '\0';
j = 0;
i--;
while (i > j)
{
temp = buff[j];
buff[j] = buff[i];
buff[i] = temp;
i--;
j++;
}
return (_writestring(buff));
}
/**
* print_pointer - prints a pointer value in hexadecimal
* @ptr: void pointer
*
* Return: void
* Convert the pointer value to a hexadecimal string using bitwise
* operations and the lookup table
*/
int print_pointer(void *ptr)
{
char table[] = "0123456789abcdef", digit;
int index, i, j, padding;
unsigned long val = (unsigned long)ptr;
char hex_str[18], truncated[18] = {'0', 'x'};
if (ptr == NULL)
return (write(1, "(nil)", 6));
for (i = 0; i < 16; i++)
{
index = val & 0xf;
digit = table[index];
hex_str[17 - i] = digit;
val >>= 4;
}
j = 2;
padding = 1;
while (j < 18)
{
if (padding && hex_str[j] == '0')
{
j++;
continue;
}
else if (padding)
{
padding = 0;
}
truncated[j] = hex_str[j];
j++;
}
truncated[j] = '\0';
return (write(1, truncated, j));
}