-
-
Save cesquivias/0ecdac66e1def41eb66b47dc461388d0 to your computer and use it in GitHub Desktop.
A simple binary compiler. Converts hexadecimal digits into a binary file. Supports comments
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
#!/usr/bin/env python | |
import os.path | |
import struct | |
def compile_line(line, i): | |
no_comment = line.split('#', 1)[0] | |
hex_code = no_comment.strip().replace(' ', '') | |
if len(hex_code) % 2 != 0: | |
raise Exception("%d: hex characters must be in pairs\n%s" % (i, line)) | |
a = [int(hex_code[i:i+2], 16) for i in xrange(0, len(hex_code), 2)] | |
return bytearray(a) | |
def main(filename, out_filename): | |
hxl_file = open(filename, 'r') | |
bin_file = open(out_filename, 'wb') | |
for i, line in enumerate(hxl_file): | |
bin_file.write(compile_line(line, i)) | |
hxl_file.close() | |
bin_file.close() | |
if __name__ == '__main__': | |
import sys | |
filename = sys.argv[1] | |
main(filename, sys.argv[2] if len(sys.argv) > 2 else os.path.splitext(filename)[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this so I could write/modify a binary file without having to use a hex editor. I want to write a JVM class file on the binary level but be able to insert comments between bytes. So I can write a class file as