Skip to content

Instantly share code, notes, and snippets.

@mgeeky
Created January 3, 2021 19:06
Show Gist options
  • Save mgeeky/eeb610c901cc6cc3d06e771ccb5a8942 to your computer and use it in GitHub Desktop.
Save mgeeky/eeb610c901cc6cc3d06e771ccb5a8942 to your computer and use it in GitHub Desktop.
Hexdump implementation in Python
def hexdump(data, addr = 0, num = 0):
s = ''
n = 0
lines = []
if num == 0: num = len(data)
if len(data) == 0:
return '<empty>'
for i in range(0, num, 16):
line = ''
line += '%04x | ' % (addr + i)
n += 16
for j in range(n-16, n):
if j >= len(data): break
line += '%02x ' % (data[j] & 0xff)
line += ' ' * (3 * 16 + 7 - len(line)) + ' | '
for j in range(n-16, n):
if j >= len(data): break
c = data[j] if not (data[j] < 0x20 or data[j] > 0x7e) else '.'
line += '%c' % c
lines.append(line)
return '\n'.join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment