Skip to content

Instantly share code, notes, and snippets.

@dev-zzo
Created August 25, 2017 06:37
Show Gist options
  • Save dev-zzo/de29434740d6c6642ccff90061fffd87 to your computer and use it in GitHub Desktop.
Save dev-zzo/de29434740d6c6642ccff90061fffd87 to your computer and use it in GitHub Desktop.
def hexdump(data):
"""Pretty print a hex dump of data, similar to xxd"""
lines = []
offset = 0
while offset < len(data):
piece = data[offset:offset + 16]
bytes = ''.join([('%02x ' % ord(x)) for x in piece])
chars = ''.join([(x if 0x20 < ord(x) < 0x7f else '.') for x in piece])
lines.append('%04x %-24s %-24s %-16s' % (offset, bytes[:24], bytes[24:], chars))
offset += len(piece)
return "\n".join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment