| Revision 69,
1.3 KB
checked in by jtorres, 9 years ago
(diff) |
|
checking in
|
| Rev | Line | |
|---|
| 1 | #include <stdio.h> |
|---|
| 2 | |
|---|
| 3 | void hexDump (char *desc, void *addr, int len) { |
|---|
| 4 | int i; |
|---|
| 5 | unsigned char buff[17]; |
|---|
| 6 | unsigned char *pc = (unsigned char*)addr; |
|---|
| 7 | |
|---|
| 8 | // Output description if given. |
|---|
| 9 | if (desc != NULL) |
|---|
| 10 | printf ("%s:\n", desc); |
|---|
| 11 | |
|---|
| 12 | if (len == 0) { |
|---|
| 13 | printf(" ZERO LENGTH\n"); |
|---|
| 14 | return; |
|---|
| 15 | } |
|---|
| 16 | if (len < 0) { |
|---|
| 17 | printf(" NEGATIVE LENGTH: %i\n",len); |
|---|
| 18 | return; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | // Process every byte in the data. |
|---|
| 22 | for (i = 0; i < len; i++) { |
|---|
| 23 | // Multiple of 16 means new line (with line offset). |
|---|
| 24 | |
|---|
| 25 | if ((i % 16) == 0) { |
|---|
| 26 | // Just don't print ASCII for the zeroth line. |
|---|
| 27 | if (i != 0) |
|---|
| 28 | printf (" %s\n", buff); |
|---|
| 29 | |
|---|
| 30 | // Output the offset. |
|---|
| 31 | printf (" %04x ", i); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | // Now the hex code for the specific character. |
|---|
| 35 | printf (" %02x", pc[i]); |
|---|
| 36 | |
|---|
| 37 | // And store a printable ASCII character for later. |
|---|
| 38 | if ((pc[i] < 0x20) || (pc[i] > 0x7e)) |
|---|
| 39 | buff[i % 16] = '.'; |
|---|
| 40 | else |
|---|
| 41 | buff[i % 16] = pc[i]; |
|---|
| 42 | buff[(i % 16) + 1] = '\0'; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | // Pad out last line if not exactly 16 characters. |
|---|
| 46 | while ((i % 16) != 0) { |
|---|
| 47 | printf (" "); |
|---|
| 48 | i++; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // And print the final ASCII bit. |
|---|
| 52 | printf (" %s\n", buff); |
|---|
| 53 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.