This is one of those generic functions that I use in practically all my reversing projects. The first argument is an adress in memory and the second argument specifies the number of bytes to show.
#include <inttypes.h>
void printHex(const void *lpvbits, const unsigned int n) {
char* data = (char*) lpvbits;
unsigned int i = 0;
char line[17] = {};
printf("%016" PRIXPTR " | ", (uintptr_t)data);
while ( i < n ) {
line[i%16] = *(data+i);
if ((line[i%16] < 32) || (line[i%16] > 126)) {
line[i%16] = '.';
}
printf("%.2X", (unsigned char)*(data+i));
i++;
if (i%4 == 0) {
if (i%16 == 0) {
if (i < n-1)
printf(" | %s\n%016" PRIXPTR " | ", (char*)&line, (uintptr_t)data+i);
} else {
printf(" ");
}
}
}
while (i%16 > 0) {
(i%4 == 0) ? printf(" ") : printf(" ");
line[i%16] = ' ';
i++;
}
printf(" | %s\n", (char*)&line);
}