Hex dump from memory location

Robert picture Robert · Aug 17, 2009 · Viewed 10.2k times · Source

I use a pointer to specify some kind of "shared memory" which I use to exchange data between different processes/threads. Now I would like to have a hex dump of the content of the shared buffer. Does anyone know how to do that?

thanks, R

Answer

anon picture anon · Aug 17, 2009

Use casts, of course :-) The function should look something like this:

void Dump( const void * mem, unsigned int n ) {
  const char * p = reinterpret_cast< const char *>( mem );
  for ( unsigned int i = 0; i < n; i++ ) {
     std::cout << hex << int(p[i]) << " ";
  }
  std::cout << std::endl;
}

Then in use:

Foo * f = GetSharedFoo();
Dump( f, somesize );

where somesize is how much you want to dump.