File tree Expand file tree Collapse file tree 1 file changed +6
-3
lines changed Expand file tree Collapse file tree 1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -42,9 +42,10 @@ def get_hex_color(byte: int) -> str:
4242 return c_ (f"{ byte :02x} " , bold = True )
4343
4444
45- def hexdump (data : bytes , length : int = 16 ) -> str :
45+ def hexdump (data : bytes , length : int = 16 , binary = False ) -> str :
4646 """
4747 Returns a formatted hex dump of the given binary data.
48+ If `binary` is True, it will return a binary dump instead of hex.
4849
4950 Args:
5051 data (bytes): The binary data to be dumped.
@@ -54,9 +55,11 @@ def hexdump(data: bytes, length: int = 16) -> str:
5455 str: A formatted hex dump of the binary data.
5556 """
5657 result = []
58+ data_len = length * 3 if not binary else length * 9
5759 for i in range (0 , len (data ), length ):
5860 chunk = data [i : i + length ]
59- hex_part = " " .join (f"{ get_hex_color (byte )} " for byte in chunk )
61+ hex_part = " " .join (f"{ get_hex_color (byte )} " for byte in chunk ) if not binary else " " .join (f"{ byte :08b} " for byte in chunk )
62+ hex_part = hex_part + " " * (data_len - (len (chunk ) * (3 if not binary else 9 )))
6063 ascii_part = "" .join ((c_ (chr (byte ), "green" ) if 32 <= byte < 127 else "." ) for byte in chunk )
61- result .append (f"{ i :08x} : { hex_part :<{length * 3 }} { ascii_part } " )
64+ result .append (f"{ i :08x} : { hex_part :<{data_len }} { ascii_part } " )
6265 return "\n " .join (result )
You can’t perform that action at this time.
0 commit comments