Skip to content

Instantly share code, notes, and snippets.

@kfxhjz
Created April 12, 2019 16:54
Show Gist options
  • Save kfxhjz/c50049aaaecd11d7c62d76a2bc7ac99b to your computer and use it in GitHub Desktop.
Save kfxhjz/c50049aaaecd11d7c62d76a2bc7ac99b to your computer and use it in GitHub Desktop.
memory hex dump
void HexDump(const char *buf, int len, int addr) {
int i, j, k, n;
char binstr[80];
n = 0;
for (i = 0; i < len; i++) {
if (0 == (i % 16)) {
n += sprintf(binstr+n, "%08x -", i + addr);
}
n += sprintf(binstr+n, " %02x", (unsigned char)buf[i]);
if (15 == (i % 16)) {
n += sprintf(binstr+n, " ");
for (j = i - 15; j <= i; j++) {
n += sprintf(binstr+n, "%c",
('!' <= buf[j] && buf[j] <= '~') ? buf[j] : '.');
}
printf("%s\n", binstr);
n = 0;
}
}
if (0 != (i % 16)) {
k = 16 - (i % 16);
for (j = 0; j < k; j++) {
n += sprintf(binstr+n, " ");
}
n += sprintf(binstr+n, " ");
k = 16 - k;
for (j = i - k; j < i; j++) {
n += sprintf(binstr+n, "%c",
('!' <= buf[j] && buf[j] <= '~') ? buf[j] : '.');
}
printf("%s\n", binstr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment