Skip to content

Instantly share code, notes, and snippets.

@phako
Last active December 27, 2021 13:07
Show Gist options
  • Save phako/96b36b5070beaf7eee27 to your computer and use it in GitHub Desktop.
Save phako/96b36b5070beaf7eee27 to your computer and use it in GitHub Desktop.
Hexdump a memory buffer in vala with address and printable dump
void hexdump (uint8[] data) {
var builder = new StringBuilder.sized (16);
var i = 0;
foreach (var c in data) {
if (i % 16 == 0) {
print ("%08x | ", i);
}
i++;
print ("%02x ", c);
if (((char) c).isprint ()) {
builder.append_c ((char) c);
} else {
builder.append (".");
}
if (i % 16 == 0) {
print ("| %s\n", builder.str);
builder.erase ();
}
}
if (i % 16 != 0) {
print ("%s| %s\n", string.nfill ((16 - (i % 16)) * 3, ' '), builder.str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment