hex2bin.py 642 B

123456789101112131415161718192021222324
  1. import sys
  2. import os
  3. import binascii
  4. import re
  5. def clean_line(string):
  6. comment_re = re.compile("/\*.*?\*/") # matches C-style comments /* */ at the end of a line
  7. return re.sub(comment_re, "" ,string).replace(' ','').replace('\n','')
  8. if len(sys.argv) < 2:
  9. print("Usage: python hex2bin.py file1.hex file2.hex ...")
  10. exit(0)
  11. filenames = sys.argv[1:]
  12. for f in filenames:
  13. bn = os.path.basename(f)
  14. with open(f) as ff:
  15. with open(bn[:-4] + ".bin", 'w') as out:
  16. lines = ff.readlines()
  17. for l in lines:
  18. c = clean_line(l)
  19. out.write(binascii.unhexlify(c))