Skip to content

Instantly share code, notes, and snippets.

@Henistein
Created July 4, 2021 16:58
Show Gist options
  • Save Henistein/70874ec53feab136330a590e5b27f680 to your computer and use it in GitHub Desktop.
Save Henistein/70874ec53feab136330a590e5b27f680 to your computer and use it in GitHub Desktop.
Simple hexdump made in python 3 (similiar to xxd)
#! /usr/bin/python3
import sys
inf = sys.stdin.buffer
data = []
byte = ''
# Read data from file in blocks of 16 bytes
# Python >= 3.8
while (byte := inf.read(16)):
data.append(byte)
# Python < 3.8
'''
byte = inf.read(16)
data.append(byte)
while byte:
byte = inf.read(16)
data.append(byte)
'''
def parse_utf(line):
res = []
for i in line:
if i in range(32, 127):
res.append(chr(i))
else:
res.append('.')
return res
count = 0
for line in data:
i = 0
j = 2
aux = line
l = len(aux)
res = []
while i <= l:
res.append(aux[i:j].hex())
i += 2
j += 2
print("{c}: {res:<40} {s}".format(c=(hex(count)[2:]).zfill(8), res=' '.join(res), s=''.join(parse_utf(line))))
count += 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment