-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printAsciiHex.c
66 lines (63 loc) · 1.03 KB
/
printAsciiHex.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
#include <stdio.h>
#include "printAsciiHex.h"
void printAsciiHex(char* buffer, int nread)
{
int i,j;
for(i=0; i<16; i++)
{
printf("%02x", i);
if(i%2==1)
printf(" ");
}
putchar('\t');
for(i=0; i<16; i++)
{
printf("%1x", i);
}
putchar('\n');
for(i=0; i<16; i++)
{
printf("__");
if(i%2==1)
printf(" ");
}
putchar('\t');
for(i=0; i<16; i++)
{
putchar('_');
}
putchar('\n');
for(i = 1; i <= nread; i++)
{
printf("%02x", buffer[i - 1] & (0xFF));
if(i%2==0 && i!=0)
printf(" ");
if(i%COLUMN_SIZE == 0)
{
putchar('\t');
for(j=i-COLUMN_SIZE; j<i; j++)
{
if(buffer[j] >= 32 && buffer[j] <= 126)
putchar(buffer[j]);
else
putchar('.');
}
putchar('\n');
}
else if(i==nread)
{
for(j=0; j<(COLUMN_SIZE-(nread%COLUMN_SIZE))*3; j++)
putchar(' ');
putchar('\t');
for(j=(nread/COLUMN_SIZE)*COLUMN_SIZE; j<nread; j++)
{
if(buffer[j] >= 32 && buffer[j] <= 126)
putchar(buffer[j]);
else
putchar('.');
}
putchar('\n');
}
}
putchar('\n');
}