-
Notifications
You must be signed in to change notification settings - Fork 0
/
104-print_buffer.c
78 lines (71 loc) · 1.3 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "main.h"
#include <stdio.h>
/**
* isPrintableASCII - determines if n is a printable ASCII char
* @n: integer
* Return: 1 if true, 0 if false
*/
int isPrintableASCII(int n)
{
return (n >= 32 && n <= 126);
}
/**
* printHexes - print hex values for string b in formatted form
* @b: string to print
* @start: starting position
* @end: ending position
*/
void printHexes(char *b, int start, int end)
{
int i = 0;
while (i < 10)
{
if (i < end)
printf("%02x", *(b + start + i));
else
printf(" ");
if (i % 2)
printf(" ");
i++;
}
}
/**
* printASCII - print ascii values for string b,
* formatted to replace nonprintable chars with '.'
* @b: string to print
* @start: starting position
* @end: ending position
*/
void printASCII(char *b, int start, int end)
{
int ch, i = 0;
while (i < end)
{
ch = *(b + i + start);
if (!isPrintableASCII(ch))
ch = 46;
printf("%c", ch);
i++;
}
}
/**
* print_buffer - prints a buffer
* @b: string
* @size: size of buffer
*/
void print_buffer(char *b, int size)
{
int start, end;
if (size > 0)
{
for (start = 0; start < size; start += 10)
{
end = (size - start < 10) ? size - start : 10;
printf("%08x: ", start);
printHexes(b, start, end);
printASCII(b, start, end);
printf("\n");
}
} else
printf("\n");
}