Skip to content

Instantly share code, notes, and snippets.

@kostyll
Created October 2, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostyll/bafe3a3e8e221418f63522ac69624446 to your computer and use it in GitHub Desktop.
Save kostyll/bafe3a3e8e221418f63522ac69624446 to your computer and use it in GitHub Desktop.
#include <ctype.h>
#include <stdio.h>
void hexdump(void *mem, unsigned int len, int cols_count)
{
unsigned int i, j;
for(i = 0; i < len + ((len % cols_count) ? (cols_count - len % cols_count) : 0); i++)
{
/* print offset */
if(i % cols_count == 0)
{
printf("0x%06x: ", i);
}
/* print hex data */
if(i < len)
{
printf("%02x ", 0xFF & ((char*)mem)[i]);
}
else /* end of block, just aligning for ASCII dump */
{
printf(" ");
}
/* print ASCII dump */
if(i % cols_count == (cols_count - 1))
{
for(j = i - (cols_count - 1); j <= i; j++)
{
if(j >= len) /* end of block, not really printing */
{
putchar(' ');
}
else if(isprint(((char*)mem)[j])) /* printable char */
{
putchar(0xFF & ((char*)mem)[j]);
}
else /* other char */
{
putchar('.');
}
}
putchar('\n');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment