"""HexDump — hex encoding/decoding utilities. Ported from net.i2p.util.HexDump. """ class HexDump: """Hex dump formatting similar to `hexdump -C`.""" @staticmethod def dump(data: bytes, offset: int = 0, length: int = -1) -> str: if length < 0: length = len(data) - offset lines = [] for i in range(0, length, 16): chunk = data[offset + i : offset + min(i + 16, length)] hex_part = " ".join(f"{b:02x}" for b in chunk) ascii_part = "".join( chr(b) if 32 <= b < 127 else "." for b in chunk ) lines.append(f"{i:08x} {hex_part:<48s} |{ascii_part}|") return "\n".join(lines) @staticmethod def to_hex(data: bytes) -> str: """Convert bytes to hex string.""" return data.hex() @staticmethod def from_hex(hex_str: str) -> bytes: """Convert hex string to bytes.""" return bytes.fromhex(hex_str)