Skip to content

Instantly share code, notes, and snippets.

@volf52
Last active September 2, 2017 17:45
Show Gist options
  • Save volf52/4c1a06927b8605539bee61e2b45ec7be to your computer and use it in GitHub Desktop.
Save volf52/4c1a06927b8605539bee61e2b45ec7be to your computer and use it in GitHub Desktop.
Create hexdump of a string - Python
def hexdump(src, length=16):
'''
:param src: https://github.com/ActiveState/code/tree/master/recipes/Python/142812_Hex_dumper/
'''
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
N=0
result=''
while src:
s,src = src[:length],src[length:]
hexa = ' '.join(["%02X"%ord(x) for x in s])
s = s.translate(FILTER)
result += "%04X %-*s %s\n" % (N, length*3, hexa, s)
N+=length
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment