Hex Memory Dump

Here is a nice looking hex dump for those interested.

ADDR | B0 B1 B2 B3 B4 B5 B6 B7  B8 B9 BA BB BC BD BE BF | 0123456789ABCDEF
=====|==================================================|=================
0000 | FF D8 FF E0 00 10 4A 46  49 46 00 01 02 01 00 48 | ......JFIF.....H
0010 | 00 48 00 00 FF ED 0A 96  50 68 6F 74 6F 73 68 6F | .H......Photosho
0020 | 70 20 33 2E 30 00 38 42  49 4D 04 04 07 43 61 70 | p 3.0.8BIM...Cap

/******************************************************************************
  Hex Memory Dump
     Usage: Dump a block of memory to the display.
    Output: &lt;0xMLOC> | <b0 -7> <b8 -15> | <c0 -7>
******************************************************************************/
void Memory_Dump(void* block, size_t size)
{
  char ASCII[] = "0123456789ABCDEF";
  unsigned int i = 0;
  unsigned int j = 0;
 
  PrintString("\nMemory Dump of ");
  PrintHex((unsigned int)block);
 
  //Trim the request to only high bytes
  block = (char*)((unsigned int)0xFFF0 & (unsigned int)block);
 
  //If you the length was 0 display at least one line
  if(size == 0)
  {
    size = 1;
  }
 
  //For each location starting from the block and going the length  
  PrintString("\nADDR | B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF | 0123456789ABCDEF");
  PrintString("\n=====|==================================================|=================");
  for( ; i < = size ; ++i )
  {
    //Display the memory location in hex
    PrintCharacter('\n');
    PrintHex((unsigned int)block+i, 4);
    PrintString(" | ");
 
    //Display the byte in hex
    j = i;
    for(; j <= i + 0xF ; ++j )
    {
      //Take the location adds the offset remove the lower byte, shift it down and convert to ASCII
      PrintCharacter(ASCII[((*(unsigned char*)((unsigned int)block+(unsigned int)j))&0xF0)>>4]);
      //Take the location adds the offset remove the upper byte and convert to ASCII
      PrintCharacter(ASCII[(*(unsigned char*)((unsigned int)block+(unsigned int)j))&0x0F]);
 
      PrintCharacter(' ');
 
      // After 8 bytes place a space for readability
      if( j == i + 0x7 )
      {  
        PrintCharacter(' ');
      }
    }    
 
    //Display the char values of the location
    PrintString("| ");
    j = i;
    for(; j < = i + 0xF ; ++j )
    {
      //If the current is a backspace, return, or tab display a space
      if(  *(unsigned char*)((unsigned int)block+(unsigned int)j) >= 0x8 
        && *(unsigned char*)((unsigned int)block+(unsigned int)j) < = 0xD )
      {
        PrintCharacter(' ');
      }
      else
      {
        PrintCharacter(*(unsigned char*)((unsigned int)block+(unsigned int)j));
      }
    }
 
    //Increment the current location
    i += 0xF;
  }
  PrintCharacter('\n');
}

Leave a Reply