Created
June 12, 2026 16:57
-
-
Save uplime/cae5dfaf5f07b73629c73b27aaddb275 to your computer and use it in GitHub Desktop.
Display an input as all hexidecimal values.
This file contains hidden or 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
| import sys | |
| def padlen(number): | |
| return ((16 - (number % 16)) * 3) + 1 + int(number % 16 < 8) | |
| def hexdump(data): | |
| printed = [] | |
| print(f"{0:08x}", end=" ") | |
| for idx in range(len(data)): | |
| print(f"{data[idx]:02x}", end=" ") | |
| if data[idx] < 128 and chr(data[idx]).isprintable(): | |
| printed.append(chr(data[idx])) | |
| else: | |
| printed.append(".") | |
| if (idx + 1) % 8 == 0: | |
| print(end=" ") | |
| if idx == len(data) - 1: | |
| padding = "" | |
| if (idx + 1) % 16 != 0: | |
| padding = " " * padlen(idx + 1) | |
| print(f"{padding}|{''.join(printed)}|") | |
| print(f"{idx + 1:08x}") | |
| elif (idx + 1) % 16 == 0: | |
| print(f"|{''.join(printed)}|") | |
| print(f"{idx + 1:08x}", end=" ") | |
| printed.clear() | |
| if len(sys.argv) > 1: | |
| with open(sys.argv[1], "rb") as handle: | |
| data = handle.read() | |
| hexdump(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment