hex2bin.py 866 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. import sys
  6. import os
  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) as ff:
  19. with open(bn[:-4] + ".bin", 'w') as out:
  20. lines = ff.readlines()
  21. for l in lines:
  22. c = clean_line(l)
  23. out.write(binascii.unhexlify(c))