hex2bin.py 843 B

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