Last active
July 11, 2021 00:06
convert a hex dump to binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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