Hexdump Utility
The `hexdump` Python library (version 3.3) provides functionality to dump binary data into a human-readable hexadecimal format and to restore binary data from such hex dumps. It works across Linux, Windows, and OS X, serving as both a Python library and a command-line tool. The last official release was in January 2016, indicating a maintenance phase rather than active development, though it remains functional for its stated purpose.
Warnings
- breaking Python 3 compatibility requires `bytes` objects for `hexdump()` input. Unlike Python 2 where `str` could be used for binary data, passing a Unicode `str` in Python 3 will result in a `TypeError`.
- gotcha When installed via an OS package manager, the command-line utility for `hexdump` might conflict with the system's `util-linux hexdump` command. The project suggests a different name like `hexdumper` for such installations.
- gotcha The `dehex()` function is designed to be more tolerant of whitespace and linefeeds in input hex strings compared to stricter alternatives like `binascii.unhexlify()`. While often convenient, this behavior might be unexpected if very strict parsing of hex data is required.
Install
-
pip install hexdump
Imports
- hexdump
import hexdump; hexdump.hexdump(some_string)
from hexdump import hexdump
- dehex
import hexdump; hexdump.dehex(some_string)
from hexdump import dehex
Quickstart
import hexdump
# Dump bytes to hex string, output to stdout
data_to_dump = b"Hello, hexdump library! This is some binary data to inspect."
print("\n--- Hex dump of data ---")
hexdump.hexdump(data_to_dump)
# Restore binary data from a hex string
hex_string_to_restore = "48 65 6C 6C 6F 2C 20 68 65 78 64 75 6D 70 20 6C 69 62 72 61 72 79 21 20 54 68 69 73 20 69 73 20 73 6F 6D 65 20 62 69 6E 61 72 79 20 64 61 74 61 20 74 6F 20 69 6E 73 70 65 63 74 2E"
restored_data = hexdump.dehex(hex_string_to_restore)
print("\n--- Restored data ---")
print(restored_data)
# You can also get the hexdump as a string instead of printing directly
hex_lines = []
for line in hexdump.dumpgen(data_to_dump):
hex_lines.append(line)
print("\n--- Hex dump as list of strings ---")
print("\n".join(hex_lines))