-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path104-print_buffer.c
53 lines (41 loc) · 983 Bytes
/
104-print_buffer.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
#include "main.h"
#include <stdio.h>
/**
* print_buffer - Prints a buffer 10 bytes at a time, starting with
* the byte position, then showing the hex content,
* then displaying printable charcaters.
* @b: The buffer to be printed.
* @size: The number of bytes to be printed from the buffer.
*/
void print_buffer(char *b, int size)
{
int byte, index;
for (byte = 0; byte < size; byte += 10)
{
printf("%08x: ", byte);
for (index = 0; index < 10; index++)
{
if ((index + byte) >= size)
printf(" ");
else
printf("%02x", *(b + index + byte));
if ((index % 2) != 0 && index != 0)
printf(" ");
}
for (index = 0; index < 10; index++)
{
if ((index + byte) >= size)
break;
else if (*(b + index + byte) >= 31 &&
*(b + index + byte) <= 126)
printf("%c", *(b + index + byte));
else
printf(".");
}
if (byte >= size)
continue;
printf("\n");
}
if (size <= 0)
printf("\n");
}