Skip to content

Instantly share code, notes, and snippets.

@ccbrown
Last active October 16, 2024 07:37
Show Gist options
  • Save ccbrown/9722406 to your computer and use it in GitHub Desktop.
Save ccbrown/9722406 to your computer and use it in GitHub Desktop.
Compact C Hex Dump Function w/ASCII
#include <stdio.h>
void DumpHex(const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
@linux4life798
Copy link

Thanks!

@kurotych
Copy link

Nice πŸ‘

@benpeoples
Copy link

Brilliant, thanks!

@fkirc
Copy link

fkirc commented Oct 25, 2018

Thanks!

@satvug
Copy link

satvug commented Nov 20, 2018

Perfect! Thank you

@eduard93
Copy link

Thank you!

Modified your code a little to return char* instead of writing to device here.

@rmacc19
Copy link

rmacc19 commented Dec 23, 2019

thanks
πŸ‘

@Flameeyes
Copy link

@ccbrown any chance you could attach a license of some kind to this code? :)

@ccbrown
Copy link
Author

ccbrown commented May 12, 2020

@Flameeyes my intention when I created this gist was that people would do whatever they want with it. Not gonna add this directly to the gist, but consider it WTFPL. :)

@scaramancha
Copy link

Oh, thanks )

@funkyweasle
Copy link

Hi All, Thanks ccbrown. Very good work.
If you are interested in enabling column specification to this code try these modifications:

  1. Add #include <stdlib.h>
  2. Change Line 3: void DumpHex(const void* data, size_t size, size_t columns) {
  3. Replace line 4: with :
    size_t half_columns = (columns/2);
    columns = half_columns * 2;
    char *ascii = (char *) malloc(columns+1);
  4. Change both occurrences of "8" with 'half_columns' and the 8 occurrences of "16" with 'columns'
  5. Finally insert this after line 29:
    free(ascii);

Enjoy.

@Hermxy
Copy link

Hermxy commented Jan 25, 2023

Thank you πŸ‘πŸ‘πŸ‘

@0xNULLderef
Copy link

i copied this snippet so many times, it's actually amazing- thank you! really useful when debugging random byte crap :>

@david232818
Copy link

david232818 commented Feb 8, 2024

Thank you for sharing this!! πŸ‘

I used this code for debugging in my personal project here and blog post that explains it.

@jwalkerbg
Copy link

jwalkerbg commented Feb 21, 2024

Thank you for this useful function.
I revised it and removed usage of printf function. Instead, a string buffer (strBuffer) is used for each line.
Also, DumpHex has third parameter - a pointer to a function that outputs the lines to the output (or to a file, or to a serial port or where the user wants to).

Renamed function to dump_hex.

Here is the link.

@itaidagan
Copy link

You don't know the number of times I came back here and copied this code, much appreciated! πŸ™πŸ™πŸ™

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment