- inline char hdigit(int n){
- return "0123456789abcdef"[n&0xf];
- };
- void dump_mem(const void *p, size_t size)
- {
- char line[49];
- char txt[17];
- const char *data = p, *pos = p;
- size_t i = 0, k;
- unsigned char c;
- while(i < size) {
- c = data[i];
- k = (i % 16) * 3;
- line[k] = c < 0x10 ? '0' : hdigit(c >> 4);
- line[k + 1] = hdigit(c);
- line[k + 2] = ' ';
- txt[i % 16] = isprint(c) ? c : '.';
- if (++i % 16 == 0) {
- line[k + 3] = 0;
- txt[16] = 0;
- printf("%p %s%s\\n", pos, line, txt);
- pos += 16;
- }
- }
- if (i % 16 != 0) {
- memset(&line[k + 3], ' ', 48 - k + 3);
- line[48] = 0;
- txt[i % 16] = 0;
- printf("%p %s%s\\n", pos, line, txt);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201410640.html
来源: http://www.codesnippet.cn/detail/1510201410640.html