/********************************************************************************* * Copyright: (C) 2012 Guo Wenxue * All rights reserved. * * Filename: dump.c * Description: This is the buffer dump print code. * * Version: 1.0.0(08/08/2012~) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "08/08/2012 06:51:40 PM" * ********************************************************************************/ #include void dump_buf(const char *prompt, const unsigned char *buf, size_t len) { char line[256]; size_t i, j; int offset; if( prompt ) { printf("%s", prompt); } for(i = 0; i < len; i += 16) { offset = snprintf(line, sizeof(line), "%08zx: ", i); /* Print hex representation */ for (j = 0; j < 16; j++) { if (i + j < len) offset += snprintf(line + offset, sizeof(line) - offset, "%02x ", buf[i + j]); else offset += snprintf(line + offset, sizeof(line) - offset, " "); } offset += snprintf(line + offset, sizeof(line) - offset, " "); /* Print ASCII representation */ for (j = 0; j < 16; j++) { if (i + j < len) { unsigned char c = buf[i + j]; offset += snprintf(line + offset, sizeof(line) - offset, "%c", (c >= 32 && c <= 126) ? c : '.'); } else { offset += snprintf(line + offset, sizeof(line) - offset, " "); } } /* Print the line */ printf("%s\n", line); } } int main (int argc, char **argv) { char buf[256]; int i; for(i=0; i