//feel free to use it in your project with nothing guaranteed
//mail to: bruceadi(at)hotmail.com
#include <cstdio>
//assumption:
// f is allowed to write,
// memory block [bytes, bytes+ len) is a accessible for current process
void dump_memory(
FILE* f,
void* bytes, size_t len
)
{
static char const* const hex = "0123456789abcdef";
unsigned char* p = (unsigned char*)bytes;
for(size_t r = len >> 3; r > 0; --r)
{
fprintf(f, "0x%p %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c\n", p,
hex[(p[0] & 0xF0) >> 4], hex[p[0] & 0xF],
hex[(p[1] & 0xF0) >> 4], hex[p[1] & 0xF],
hex[(p[2] & 0xF0) >> 4], hex[p[2] & 0xF],
hex[(p[3] & 0xF0) >> 4], hex[p[3] & 0xF],
hex[(p[4] & 0xF0) >> 4], hex[p[4] & 0xF],
hex[(p[5] & 0xF0) >> 4], hex[p[5] & 0xF],
hex[(p[6] & 0xF0) >> 4], hex[p[6] & 0xF],
hex[(p[7] & 0xF0) >> 4], hex[p[7] & 0xF]
);
p += 8;
}
size_t n = len & 0x7;
if(n)
{
fprintf(f, "0x%p", p);
for(; n > 0; --n)
{
fprintf(f, " %c%c", hex[(*p & 0xF0) >> 4], hex[*p & 0xF]);
++p;
}
fprintf(f, "\n");
}
}
int main ()
{
int a;
dump_memory(stdout,&a, 32);
return 0;
}
Sample output:
0x0xbf9c7c7c 51 a3 04 08 00 00 0a ff
0x0xbf9c7c84 a0 7c 9c bf c8 7c 9c bf
0x0xbf9c7c8c 06 2d 15 40 24 fd 26 40
0x0xbf9c7c94 40 51 01 40 c8 7c 9c bf
0x0xbf9c7c9c 06 2d 15 40 01 00 00 00
0x0xbf9c7ca4 f4 7c 9c bf fc 7c 9c bf
0x0xbf9c7cac 00 a9 04 08 00 00 00 00
0x0xbf9c7cb4 40 aa 00 40 ec c1 26 40
0x0xbf9c7cbc c0 55 01 40 01 00 00 00
0x0xbf9c7cc4 00 a9 04 08 00 00 00 00
0x0xbf9c7ccc 21 a9 04 08 b6 ab 04 08
0x0xbf9c7cd4 01 00 00 00 f4 7c 9c bf
0x0xbf9c7cdc a0 d3 04 08