-
Notifications
You must be signed in to change notification settings - Fork 0
/
104-print_buffer.c
executable file
·57 lines (45 loc) · 1.02 KB
/
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
54
55
56
57
/*
* File: 103-print_buffer.c
* Auth: Brennan D Baraban
*/
#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");
}