Skip to content

Instantly share code, notes, and snippets.

@huzhifeng
Created July 4, 2014 06:45
Show Gist options
  • Save huzhifeng/9d65c5ffeb6d8760ae28 to your computer and use it in GitHub Desktop.
Save huzhifeng/9d65c5ffeb6d8760ae28 to your computer and use it in GitHub Desktop.
hexdump
#include <stdio.h>
#include <string.h>
extern void hexdump(FILE * stream, void const * data, unsigned int len)
{
unsigned int i;
unsigned int r,c;
if (!stream)
return;
if (!data)
return;
for (r=0,i=0; r<(len/16+(len%16!=0)); r++,i+=16)
{
fprintf(stream,"%04X: ",i); /* location of first byte in line */
for (c=i; c<i+8; c++) /* left half of hex dump */
if (c<len)
fprintf(stream,"%02X ",((unsigned char const *)data)[c]);
else
fprintf(stream," "); /* pad if short line */
fprintf(stream," ");
for (c=i+8; c<i+16; c++) /* right half of hex dump */
if (c<len)
fprintf(stream,"%02X ",((unsigned char const *)data)[c]);
else
fprintf(stream," "); /* pad if short line */
fprintf(stream," ");
for (c=i; c<i+16; c++) /* ASCII dump */
if (c<len)
if (((unsigned char const *)data)[c]>=32 &&
((unsigned char const *)data)[c]<127)
fprintf(stream,"%c",((char const *)data)[c]);
else
fprintf(stream,"."); /* put this for non-printables */
else
fprintf(stream," "); /* pad if short line */
fprintf(stream,"\n");
}
fflush(stream);
}
struct packet
{
unsigned char smac[6];
unsigned char dmac[6];
unsigned char sip[4];
unsigned char dip[4];
unsigned char data[128];
};
int main(int argc, char *argv[])
{
struct packet p = {
.smac = {0x00, 0x00, 0x00, 0x11, 0x22, 0x33},
.dmac = {0x00, 0x00, 0x00, 0x44, 0x55, 0x66},
.sip = {192, 168, 1, 10},
.dip = {192, 168, 1, 20},
};
char *str = "Hello, My name is huzhifeng!";
unsigned int len = 0;
if (argc > 1)
{
str = argv[1];
}
len = strlen(str);
hexdump(stdout, str, len);
strcpy((char *)p.data, "This is the test data: hello world");
hexdump(stdout, &p, sizeof(p));
return 0;
}
/*
[root@huzhifeng tmp]# gcc -o hexdump hexdump.c
[root@huzhifeng tmp]# ./hexdump
0000: 48 65 6C 6C 6F 2C 20 4D 79 20 6E 61 6D 65 20 69 Hello, My name i
0010: 73 20 68 75 7A 68 69 66 65 6E 67 21 s huzhifeng!
0000: 00 00 00 11 22 33 00 00 00 44 55 66 C0 A8 01 0A ...."3...DUf....
0010: C0 A8 01 14 54 68 69 73 20 69 73 20 74 68 65 20 ....This is the
0020: 74 65 73 74 20 64 61 74 61 3A 20 68 65 6C 6C 6F test data: hello
0030: 20 77 6F 72 6C 64 00 00 00 00 00 00 00 00 00 00 world..........
0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090: 00 00 00 00 ....
[root@huzhifeng tmp]# ./hexdump abcdefghijklmnopqrstuvwxyz1234567890
0000: 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 abcdefghijklmnop
0010: 71 72 73 74 75 76 77 78 79 7A 31 32 33 34 35 36 qrstuvwxyz123456
0020: 37 38 39 30 7890
0000: 00 00 00 11 22 33 00 00 00 44 55 66 C0 A8 01 0A ...."3...DUf....
0010: C0 A8 01 14 54 68 69 73 20 69 73 20 74 68 65 20 ....This is the
0020: 74 65 73 74 20 64 61 74 61 3A 20 68 65 6C 6C 6F test data: hello
0030: 20 77 6F 72 6C 64 00 00 00 00 00 00 00 00 00 00 world..........
0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090: 00 00 00 00 ....
[root@huzhifeng tmp]#
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment