add hexdump util
This commit is contained in:
15
hexdump.py
Normal file
15
hexdump.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/79038702
|
||||||
|
def hexdump(data: bytes):
|
||||||
|
def to_printable_ascii(byte):
|
||||||
|
return chr(byte) if 32 <= byte <= 126 else "."
|
||||||
|
|
||||||
|
offset = 0
|
||||||
|
while offset < len(data):
|
||||||
|
chunk = data[offset : offset + 16]
|
||||||
|
hex_values = " ".join(f"{byte:02x}" for byte in chunk)
|
||||||
|
ascii_values = "".join(to_printable_ascii(byte) for byte in chunk)
|
||||||
|
print(f"{offset:08x} {hex_values:<48} |{ascii_values}|")
|
||||||
|
offset += 16
|
||||||
|
|
Reference in New Issue
Block a user