view-hex.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #----------------------------------------------------------------------------------------------------------------------*
  4. # From: https://github.com/msolters/intel-hex-viewer
  5. #----------------------------------------------------------------------------------------------------------------------*
  6. import sys
  7. def parse_hex_line( line ):
  8. if len( current_line ) == 0: return
  9. bytecount = int( line[0:2], 16 )
  10. address = int( line[2:6], 16 )
  11. rec_type = int( line[6:8], 16 )
  12. rec_output = str(hex(address)) + '\t(' + str(bytecount) + ')\t'
  13. if rec_type == 0:
  14. rec_output += '(data)'
  15. rec_output += '\t\t' + line[8:(8+2*(bytecount))]
  16. elif rec_type == 1:
  17. rec_output += '(end of file)'
  18. elif rec_type == 2:
  19. rec_output += '(extended segment address)'
  20. elif rec_type == 3:
  21. rec_output += '(start segment address)'
  22. elif rec_type == 4:
  23. rec_output += '(extended linear address)'
  24. elif rec_type == 5:
  25. rec_output += '(start linear address)'
  26. print (rec_output)
  27. # (1) Open the Hex File
  28. hex_file_path = sys.argv[1]
  29. print ("Parsing " + hex_file_path)
  30. hex_file = open(hex_file_path, "rb")
  31. # (2) Analyze the hex file line by line
  32. current_line = ""
  33. try:
  34. byte = "1" # initial placeholder
  35. while byte != "":
  36. byte = hex_file.read(1)
  37. if byte == ":":
  38. # (1) Parse the current line!
  39. parse_hex_line( current_line )
  40. # (2) Reset the current line to build the next one!
  41. current_line = ""
  42. else:
  43. current_line += byte
  44. parse_hex_line( current_line )
  45. finally:
  46. hex_file.close()