Skip to content

Instantly share code, notes, and snippets.

@mtfurlan
Last active October 13, 2022 20:12
Show Gist options
  • Save mtfurlan/a3a44246e6ea89809a87aac3d17c5bd2 to your computer and use it in GitHub Desktop.
Save mtfurlan/a3a44246e6ea89809a87aac3d17c5bd2 to your computer and use it in GitHub Desktop.
c function to emulate xxd -g1
#include <ctype.h>
// based on https://stackoverflow.com/q/54500648/2423187
void hexDump(const uint8_t* data, int len)
{
int i;
unsigned char bufferLine[17];
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0) {
printf(" %s\n", bufferLine);
}
printf("%08x:", i);
}
printf(" %02x", data[i]);
bufferLine[i % 16] = isprint(data[i]) ? data[i] : '.';
bufferLine[(i % 16) + 1] = '\0';
}
while ((i % 16) != 0) {
printf(" ");
i++;
}
printf(" %s\n", bufferLine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment