Skip to content

Instantly share code, notes, and snippets.

@onlyforbopi
Forked from sbz/hexdump.py
Last active October 5, 2018 08:16
Show Gist options
  • Save onlyforbopi/7bd9d2c1b3c3c44a140f1079f0ffa240 to your computer and use it in GitHub Desktop.
Save onlyforbopi/7bd9d2c1b3c3c44a140f1079f0ffa240 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 range(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)
# print(hexdump("asdfasdfsdfsdfasdfasdfasdfaksdfaksdfjaksdjsfasd", length=16))
#
#0000 61 73 64 66 61 73 64 66 73 64 66 73 64 66 61 73 asdfasdfsdfsdfas
#0010 64 66 61 73 64 66 61 73 64 66 61 6b 73 64 66 61 dfasdfasdfaksdfa
#0020 6b 73 64 66 6a 61 6b 73 64 6a 73 66 61 73 64 ksdfjaksdjsfasd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment