Skip to content

Instantly share code, notes, and snippets.

@p1-olm
Last active September 17, 2018 12:50
Show Gist options
  • Save p1-olm/a2504c07046e84da8270958e2ac8c4bc to your computer and use it in GitHub Desktop.
Save p1-olm/a2504c07046e84da8270958e2ac8c4bc to your computer and use it in GitHub Desktop.
def hex_dump(pdu):
""" Useful routine to dump pdu, "xxd" way
"""
import string
length = 0x10
for i in range(0, len(pdu) // length + 1):
begin = i * length
end = (i + 1) * length
padding = " " * (length - len(pdu[begin:end])) * 3
bytes_chunk = "".join("{:02x} ".format(ord(c)) for c in pdu[begin:end])
ascii_repr = padding + "".join(a if a in string.printable[:-5] else "." for a in pdu[begin:end])
print("{:08x}: {} {}".format(begin, bytes_chunk, ascii_repr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment