Skip to content

Instantly share code, notes, and snippets.

@7h3rAm
Last active April 26, 2021 20:49
Show Gist options
  • Save 7h3rAm/5603718 to your computer and use it in GitHub Desktop.
Save 7h3rAm/5603718 to your computer and use it in GitHub Desktop.
hexdump implementation in Python
#!/usr/bin/env python3
def hexdump(src, length=16, sep='.'):
"""
>>> print(hexdump('\x01\x02\x03\x04AAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBB'))
00000000: 01 02 03 04 41 41 41 41 41 41 41 41 41 41 41 41 |....AAAAAAAAAAAA|
00000010: 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 42 |AAAAAAAAAAAAAABB|
00000020: 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 |BBBBBBBBBBBBBBBB|
00000030: 42 42 42 42 42 42 42 42 |BBBBBBBB|
>>>
>>> print(hexdump(b'\x01\x02\x03\x04AAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBB'))
00000000: 01 02 03 04 41 41 41 41 41 41 41 41 41 41 41 41 |....AAAAAAAAAAAA|
00000010: 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 42 |AAAAAAAAAAAAAABB|
00000020: 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 |BBBBBBBBBBBBBBBB|
00000030: 42 42 42 42 42 42 42 42 |BBBBBBBB|
"""
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep for x in range(256)])
lines = []
for c in range(0, len(src), length):
chars = src[c:c+length]
hexstr = ' '.join(["%02x" % ord(x) for x in chars]) if type(chars) is str else ' '.join(['{:02x}'.format(x) for x in chars])
if len(hexstr) > 24:
hexstr = "%s %s" % (hexstr[:24], hexstr[24:])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars]) if type(chars) is str else ''.join(['{}'.format((x <= 127 and FILTER[x]) or sep) for x in chars])
lines.append("%08x: %-*s |%s|" % (c, length*3, hexstr, printable))
return '\n'.join(lines)
if __name__ == "__main__":
print(hexdump('\x01\x02\x03\x04AAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBB'))
print(hexdump(b'\x01\x02\x03\x04AAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBB'))
@7h3rAm
Copy link
Author

7h3rAm commented May 18, 2013

Based on @sbz's implementation here: https://gist.github.com/sbz/1080258

[15/Oct/2019] Changed variable name from "hex" to "hexstr" (now a concatenation of two builtins :) thanks to @chrispetrou for the suggestion)
[03/Sep/2019] Updated to be compatible with Python 3 and added support for both "str" and "bytes" input (thanks to @mzpqnxow's implementation here: https://gist.github.com/mzpqnxow/a368c6cd9fae97b87ef25f475112c84c)

@joelparker
Copy link

A huge thank you for this gist. You've saved me a whole lot of time in debugging effort.

@megahall
Copy link

megahall commented Apr 8, 2014

Love it!

@rochamorelli
Copy link

Thanks, so handy!!

@1mm0rt41PC
Copy link

Same script with full support for python3 at https://gist.github.com/ImmortalPC/c340564823f283fe530b

@JonathonReinhart
Copy link

@mzpqnxow
Copy link

mzpqnxow commented Sep 2, 2019

A tweak on this for Python3, assuming input is bytes, using .format() and padding spaces into the printable column where the buffer ends out of bytes:

https://gist.github.com/mzpqnxow/a368c6cd9fae97b87ef25f475112c84c

@chrispetrou
Copy link

Really great snippet! Thank you! Just a small feedback, it would be better to rename hex to something else since hex is a python built-in function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment