Skip to content

Instantly share code, notes, and snippets.

@iccir
Created March 1, 2026 04:23
Show Gist options
  • Select an option

  • Save iccir/64b27a5693a651204bf52d10720ccb0c to your computer and use it in GitHub Desktop.

Select an option

Save iccir/64b27a5693a651204bf52d10720ccb0c to your computer and use it in GitHub Desktop.
Yet another public domain hexdump implementation
// Pubic Domain
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
static void sLogHexDump(const void *address, const void *data, size_t size)
{
// 3 bytes per hex value + middle space + terminator
const size_t hexStringLength = (3 * 16) + 1 + 1;
// 16 bytes per line + terminator
const size_t asciiStringLength = 16 + 1;
char hexString[hexStringLength];
char asciiString[asciiStringLength];
static const char nibbleLookup[] = "0123456789abcdef";
for (size_t offset = 0; offset < size; offset += 16) {
size_t rowLength = size - offset;
if (rowLength > 16) rowLength = 16;
char *h = hexString;
char *a = asciiString;
for (size_t i = 0; i < 16; i++) {
if (i == 8) {
*h++ = ' ';
}
if (i < rowLength) {
uint8_t b = ((uint8_t *)data)[offset + i];
*h++ = nibbleLookup[(b >> 4) & 0xf];
*h++ = nibbleLookup[ b & 0xf];
*a++ = isprint(b) ? b : '.';
} else {
*h++ = ' ';
*h++ = ' ';
*a++ = ' ';
}
*h++ = ' ';
}
hexString[ hexStringLength - 1] = 0;
asciiString[asciiStringLength - 1] = 0;
printf("0x%016lx %s |%s|\n", (unsigned long)address + offset, hexString, asciiString);
}
}
int main(int argc, const char * argv[])
{
size_t len = 1024 * 4;
void *buf = malloc(len);
arc4random_buf(buf, len);
sLogHexDump((void *)0xbeefbeef, buf, len);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment