Last active
September 2, 2017 17:45
-
-
Save volf52/4c1a06927b8605539bee61e2b45ec7be to your computer and use it in GitHub Desktop.
Create hexdump of a string - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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