zvodd
4/12/2017 - 4:05 AM

Reverse output of `hexdump` only showing ASCII characters (swap lastline comment for bytestring instead)

Reverse output of hexdump only showing ASCII characters (swap lastline comment for bytestring instead)

def filter_hex(istr):
	return ''.join([c for c in istr if (c >= '0' and c <= '9') 
								or (c >= "A" and c <= 'F')
								or (c >= 'a' and c <= 'f')])

skipstart = 7
skipend = None

vals = bytes() 
with open('thing.hex', 'r') as fh:
	lines = fh.read().splitlines()
	last = len(lines) -1
	for count, line in enumerate(lines):
		filtered = filter_hex(line[skipstart:skipend])
		if count == last and len(filtered) % 4 != 0:
			filtered += '0' * (len(filtered) % 4)
		for i in range(0,len(filtered), 4):
			vals += bytes([int(filtered[i+2], 16)*16 + int(filtered[i+3], 16)])
			vals += bytes([int(filtered[i], 16)*16 + int(filtered[i+1], 16)])
			
print(vals.decode("ascii", errors="ignore"))
#print(vals)