-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-print_memorytest.c
65 lines (53 loc) · 1.06 KB
/
5-print_memorytest.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
#include <unistd.h>
void ft_print_hex(int x)
{
char *base = "0123456789abcdef";
write(1, &base[x / 16], 1);
write(1, &base[x % 16], 1);
}
void ft_putchar(char c)
{
if (c >= 32 && c <= 126)
write(1, &c, 1);
else
write(1, ".", 1);
}
void print_memory(const void *addr, size_t size)
{
size_t i = 0;
size_t j;
unsigned char const *p = addr;
while (i < size)
{
j = 0;
while (j < 16 && i + j < size)
{
ft_print_hex(*(p + i + j));
if (j % 2)
write(1, " ", 1);
j++;
}
while (j < 16)
{
write (1, " ", 2);
if (j % 2)
write(1, " ", 1);
j++;
}
j = 0;
while (j < 16 && i + j < size)
{
ft_putchar(*(p + i + j));
j++;
}
write (1, "\n", 1);
i += 16;
}
}
#include <string.h>
int main(void)
{
int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
print_memory(tab, sizeof(tab));
return (0);
}