teknoraver

hexdump

Mar 29th, 2016 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. void hexdump(char *buf, size_t length)
  2. {
  3.     int star = 0;
  4.     for (size_t i = 0; i < length; i += 16, buf += 16) {
  5.         if (i && !memcmp(buf, buf - 16, 16)) {
  6.             if (!star)
  7.                 puts("*");
  8.             star = 1;
  9.             continue;
  10.         }
  11.         star = 0;
  12.         printf("%08zx  ", i);
  13.         for (size_t j = 0; j < 16; j++) {
  14.             printf("%02x ", buf[j] & 0xff);
  15.             if ((j & 7) == 7)
  16.                 putchar(' ');
  17.         }
  18.         putchar('|');
  19.         for (size_t j = 0; j < 16; j++) {
  20.             int c = buf[j];
  21.             putchar(c >= ' ' && c <= '~' ? c : '.');
  22.         }
  23.         puts("|");
  24.     }
  25. }
Add Comment
Please, Sign In to add comment