def hexdump(src, length=16, sep='.'):
"""
Displays a hex output of the content it is passed.
This was based on https://gist.github.com/7h3rAm/5603718 with some
minor modifications
"""
allowed = digits + ascii_letters + punctuation + ' '
print_map = ''.join(((x if x in allowed else '.')
for x in map(chr, 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 print_map[ord(x)]) or sep) for x in chars])
lines.append("%08x: %-*s |%s|" % (c, length * 3, hex, printable))
return '\n'.join(lines)
评论列表
文章目录