Skip to content

Instantly share code, notes, and snippets.

@AmesianX
Forked from mgeeky/hexdump.py
Created March 23, 2021 21:51
Show Gist options
  • Select an option

  • Save AmesianX/5df9626250caff8d74ac117856a33387 to your computer and use it in GitHub Desktop.

Select an option

Save AmesianX/5df9626250caff8d74ac117856a33387 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