Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/zenlib/util/hexdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def get_hex_color(byte: int) -> str:
return c_(f"{byte:02x}", bold=True)


def hexdump(data: bytes, length: int = 16) -> str:
def hexdump(data: bytes, length: int = 16, binary=False) -> str:
"""
Returns a formatted hex dump of the given binary data.
If `binary` is True, it will return a binary dump instead of hex.
Args:
data (bytes): The binary data to be dumped.
Expand All @@ -54,9 +55,11 @@ def hexdump(data: bytes, length: int = 16) -> str:
str: A formatted hex dump of the binary data.
"""
result = []
data_len = length * 3 if not binary else length * 9
for i in range(0, len(data), length):
chunk = data[i : i + length]
hex_part = " ".join(f"{get_hex_color(byte)}" for byte in chunk)
hex_part = " ".join(f"{get_hex_color(byte)}" for byte in chunk) if not binary else " ".join(f"{byte:08b}" for byte in chunk)
hex_part = hex_part + " " * (data_len - (len(chunk) * (3 if not binary else 9)))
ascii_part = "".join((c_(chr(byte), "green") if 32 <= byte < 127 else ".") for byte in chunk)
result.append(f"{i:08x}: {hex_part:<{length * 3}} {ascii_part}")
result.append(f"{i:08x}: {hex_part:<{data_len}} {ascii_part}")
return "\n".join(result)