hex2bin.py 875 B

1234567891011121314151617181920212223242526272829
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import sys
  5. import os
  6. from io import open
  7. import binascii
  8. import re
  9. def clean_line(string):
  10. comment_re = re.compile("/\*.*?\*/") # matches C-style comments /* */ at the end of a line
  11. return re.sub(comment_re, "" ,string).replace(" ","").replace("\n","")
  12. if len(sys.argv) < 2:
  13. print("Usage: python hex2bin.py file1.hex file2.hex ...")
  14. exit(0)
  15. filenames = sys.argv[1:]
  16. for f in filenames:
  17. bn = os.path.basename(f)
  18. with open(f, mode="rt") as ff:
  19. with open(bn[:-4] + ".bin", "wb") as out:
  20. lines = ff.readlines()
  21. for l in lines:
  22. c = clean_line(l)
  23. out.write(binascii.unhexlify(c))