Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Last active September 13, 2022 13:36
Show Gist options
  • Save jstaursky/3bdd5cb03931ecfbbe59039781c7f985 to your computer and use it in GitHub Desktop.
Save jstaursky/3bdd5cb03931ecfbbe59039781c7f985 to your computer and use it in GitHub Desktop.
hexdump
#include <algorithm>
#include <cctype>
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <iostream>
int main(int argc, char *argv[])
{
FILE *fp = stdin;
uint8_t buffer[16] = {0};
uintmax_t address = 0;
size_t ncolumns = 16;
for (size_t nbytes; nbytes = fread(buffer, sizeof(uint8_t), sizeof(buffer), fp); address += 0x10)
{
printf("%08" PRIxMAX ": ", address);
auto grouping = 2;
// Hexdump columns
for (size_t idx = 0; idx != ncolumns; ++idx) {
int spaces = (idx + 1) % grouping ? 0 : 1;
if (idx < nbytes)
printf("%02x" "%*s", buffer[idx], spaces, "");
else
printf(" %*s", spaces, "");
}
// ASCII column
for (size_t idx = 0; idx != nbytes; ++idx) {
printf("%c", isprint(buffer[idx]) ? buffer[idx] : '.');
}
printf("\n");
};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment