add hex dump function

This commit is contained in:
Kelvin Sherlock 2014-08-21 12:30:38 -04:00
parent f3cf2ac338
commit 1d17766135
2 changed files with 53 additions and 0 deletions

View File

@ -13,6 +13,8 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "options.h"
#include "prototypes.h"
@ -163,3 +165,51 @@ int CloseLoop(Connection *connection)
return 1;
}
void hexdump(const unsigned char *data, int size)
{
const char *HexMap = "0123456789abcdef";
static char buffer1[16 * 3 + 1 + 1];
static char buffer2[16 + 1];
Word offset = 0;
unsigned i, j;
while(size > 0)
{
unsigned linelen = 16;
memset(buffer1, ' ', sizeof(buffer1));
memset(buffer2, ' ', sizeof(buffer2));
if (size < linelen) linelen = size;
for (i = 0, j = 0; i < linelen; i++)
{
unsigned x = data[i];
buffer1[j++] = HexMap[x >> 4];
buffer1[j++] = HexMap[x & 0x0f];
j++;
if (i == 7) j++;
buffer2[i] = isprint(x) ? x : '.';
}
buffer1[sizeof(buffer1)-1] = 0;
buffer2[sizeof(buffer2)-1] = 0;
printf("%04x:\t%s\t%s\n", offset, buffer1, buffer2);
offset += 16;
data += 16;
size -= 16;
}
printf("\n");
}

View File

@ -16,6 +16,9 @@
pascal void DisplayCallback(const char *message);
void hexdump(const unsigned char *data, int size);
typedef struct ReadBlock
{