a simple function for memory dumping

//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



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值