Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active July 11, 2021 00:06
Show Gist options
  • Save arpruss/f753d6dc748abb25bcb3c23068d6f0d6 to your computer and use it in GitHub Desktop.
Save arpruss/f753d6dc748abb25bcb3c23068d6f0d6 to your computer and use it in GitHub Desktop.
convert a hex dump to binary
# Public Domain / CC0 license
import sys
import re
import zlib
infile = sys.argv[1]
outfile = sys.argv[1].replace(".txt",".bin")
data = [0 for i in range(32768)]
wrote = 0
with open(infile, "r") as inf:
with open(outfile, "wb") as f:
for line in inf:
m = re.match('([0-9a-fA-F]+):\s*([0-9a-fA-F]+)', line)
if m:
address = int(m.group(1), 16)
values = m.group(2)
for i in range(0,len(values),2):
val = int(values[i:i+2],16)
data[address] = val
address += 1
f.write(bytes(data))
headerCheck = 0
for i in range(0x134, 0x14d):
headerCheck -= data[i]+1
print("header checksum: %02x" % (headerCheck & 0xFF))
s = 0
for i in range(0x8000):
if i != 0x14e and i != 0x14f:
s += data[i]
print("ROM checksum: 0x%04x" % (s & 0xFFFF))
print("crc32: %08x" % (zlib.crc32(bytes(data))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment