Last active
December 27, 2021 13:07
-
-
Save phako/96b36b5070beaf7eee27 to your computer and use it in GitHub Desktop.
Hexdump a memory buffer in vala with address and printable dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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