Skip to content

Instantly share code, notes, and snippets.

@kogcyc
Created August 8, 2017 08:47
Show Gist options
  • Save kogcyc/2548770f31e8d264e0e61fde6c7682af to your computer and use it in GitHub Desktop.
Save kogcyc/2548770f31e8d264e0e61fde6c7682af to your computer and use it in GitHub Desktop.
def hexdump(src, length=16, sep='.'):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep 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])
if len(hex) > 24:
hex = "%s %s" % (hex[:24], hex[24:])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars])
lines.append("%08x: %-*s |%s|\n" % (c, length*3, hex, printable))
print ''.join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment