Created
January 25, 2021 20:40
-
-
Save secoats/d70898d5576fa9884145cb8e11f3a55c to your computer and use it in GitHub Desktop.
Simple Hex Dump in Python3
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
#!/usr/bin/env python3 | |
def hexdump(bytes_input, width=16): | |
current = 0 | |
end = len(bytes_input) | |
result = "" | |
while current < end: | |
byte_slice = bytes_input[current : current + width] | |
# hex section | |
for b in byte_slice: | |
result += "%02X " % b | |
# filler | |
for _ in range(width - len(byte_slice)): | |
result += " " * 3 | |
result += " " * 2 | |
# printable character section | |
for b in byte_slice: | |
if (b >= 32) and (b < 127): | |
result += chr(b) | |
else: | |
result += "." | |
result += "\n" | |
current += width | |
return result | |
dump_str = hexdump(b"example\x00\xAA\x00exampleYOHO\x00\xAA\x00exampleYOHO\x00\xAA\x0A\x00\xAA\x00A") | |
print(dump_str) | |
""" | |
Output: | |
65 78 61 6D 70 6C 65 00 AA 00 65 78 61 6D 70 6C example...exampl | |
65 59 4F 48 4F 00 AA 00 65 78 61 6D 70 6C 65 59 eYOHO...exampleY | |
4F 48 4F 00 AA 0A 00 AA 00 41 OHO......A | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment