Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 71 lines (54 sloc) 1.303 kb
#ifndef com_sleepless_hexdump_cpp
#define com_sleepless_hexdump_cpp
/* Copyright 1998-2004 Sleepless Software Inc. All Rights Reserved */
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
struct HexDump
{
static void dump16(FILE *s, int addr, const unsigned char *buf, int len)
{
// Print the address
fprintf(s, "%8.8x ", addr);
// Print the 16 bytes
int i;
for(i = 0; i < 16; i++)
{
if(i < len)
fprintf(s, "%2.2x ", buf[i] & 0xff);
else
fprintf(s, " ");
}
// Print the 16 ascii chars represented by those bytes, or a '.' for
// unprintable chars.
for(i = 0; i < 16; i++)
{
if(i < len)
{
char c = buf[i];
if((c < ' ') || (c >= 126))
c = '.';
fprintf(s, "%c", c);
}
else
fprintf(s, " ");
}
// And a newline to end the line.
fprintf(s, "\n");
}
// This takes a memory buffer and length, and sends a hex dump of
// those bytes to the given stream.
static void dump(FILE *s, int addr, const unsigned char *buf, int len)
{
int fullLines = len / 16;
int remainder = len % 16;
int i;
for(i = 0; i < fullLines; i++)
{
dump16(s, addr + (i * 16), buf + (i * 16), 16);
}
if(remainder > 0)
dump16(s, addr + (i * 16), buf + (i * 16), remainder);
}
};
#endif // com_sleepless_hexdump_cpp
Jump to Line
Something went wrong with that request. Please try again.