Skip to content

Instantly share code, notes, and snippets.

@sbz
Created July 13, 2011 13:04
Show Gist options
  • Save sbz/1080258 to your computer and use it in GitHub Desktop.
Save sbz/1080258 to your computer and use it in GitHub Desktop.
hexdump implementation in Python
def hexdump(src, length=16):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
lines.append("%04x %-*s %s\n" % (c, length*3, hex, printable))
return ''.join(lines)
@1mm0rt41PC
Copy link

1mm0rt41PC commented May 15, 2014

Very good snippet ! Thanks !
I did a port to python3: https://gist.github.com/1mm0rt41PC/c340564823f283fe530b

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