Someone on Github wrote a Python module for Intel HEX files so I cobbled this together. Not tested very much at all but definately works exactly as coded 
Python 3, pip install the library as shown in the links
Code:
# Reads an intel hex file
# Extracts data only from the data field
# Parses out the two digit ascii hex, optionally converting to three digit ascii octal
# Writes output as a space delimited ascii stream of the data
# TODO Ideally breaks the output into multiple files of a target size or at original address breaks
# TODO emit binary to file
# see https://en.wikipedia.org/wiki/Intel_HEX
# see https://pypi.org/project/IntelHex/
# see https://github.com/python-intelhex/intelhex
import sys
import getopt
from intelhex import IntelHex
def main(argv):
inputFile = ''
outputFile = ''
outputFormat = '%c ' # ascii output by default
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile=","octal","hexadecimal","ascii"])
except getopt.GetoptError:
print('hex2ascii.py [--hexadecimal,--octal, --ascii] -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('hex2ascii.py [--hex,--oct] -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputFile = arg
elif opt in ("-o", "--ofile"):
outputFile = arg
# Option to Write output as a space delimited ascii stream of the data
elif opt in ("--ascii"):
outputFormat = '%c '
# Option to Parse out the two digit ascii hex
elif opt in ("--hexadecimal"):
outputFormat = '%X '
# Optionally converting to three digit ascii octal
elif opt in ("--octal"):
outputFormat = '%o '
if len(inputFile) == 0:
print('hex2ascii.py [--hex,--oct] -i <inputfile> -o <outputfile>')
return
print('Input file is ', inputFile)
print('output format is ', outputFormat)
# Reads an intel hex file
ih = IntelHex()
ih.fromfile(inputFile, format='hex')
# Extracts data only from the data field
if len(outputFile) > 0:
# Writes to a file NOTE only output printable ascii TODO write binary
f = open(outputFile, "w")
for x in range(ih.minaddr(), ih.maxaddr()):
if ih[x] <= 127:
f.write(outputFormat% (ih[x]))
else:
f.write('. ') # print dot instead for now
f.close()
print('Output file is ', outputFile)
else:
# Writes to console
for x in range(ih.minaddr(), ih.maxaddr()):
print(outputFormat% (ih[x]), end='')
print()
if __name__ == "__main__":
main(sys.argv[1:])
Running it (Intel HEX sample from Wikipedia)
Code:
C:\Users\you\Documents\hex2ascii>cat foo.hex
:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF
C:\Users\you\Documents\hex2ascii>python hex2ascii.py -i foo.hex
Input file is foo.hex
output format is %c
! F 6 ! G 6 ~ þ Ò ! F ~ Â ÿ _ ! H N y # F # – W x # ž Ú ? ² Ê ? V p + ^ q + r + s ! F 4
C:\Users\you\Documents\hex2ascii>python hex2ascii.py -i foo.hex --octal
Input file is foo.hex
output format is %o
41 106 1 66 1 41 107 1 66 0 176 376 11 322 31 1 41 106 1 176 27 302 0 1 377 137 26 0 41 110 1 31 31 116 171 43 106 43 226 127 170 43 236 332 77 1 262 312 77 1 126 160 53 136 161 53 162 53 163 41 106 1 64
C:\Users\you\Documents\hex2ascii>python hex2ascii.py -i foo.hex --hexadecimal
Input file is foo.hex
output format is %X
21 46 1 36 1 21 47 1 36 0 7E FE 9 D2 19 1 21 46 1 7E 17 C2 0 1 FF 5F 16 0 21 48 1 19 19 4E 79 23 46 23 96 57 78 23 9E DA 3F 1 B2 CA 3F 1 56 70 2B 5E 71 2B 72 2B 73 21 46 1 34
C:\Users\you\Documents\hex2ascii>python hex2ascii.py -i foo.hex -o out.txt
Input file is foo.hex
output format is %c
Output file is out.txt
C:\Users\you\Documents\hex2ascii>cat out.txt
! F 6 ! G 6 ~ . . ! F ~ . . _ ! H N y # F # . W x # . . ? . . ? V p + ^ q + r + s ! F 4
C:\Users\you\Documents\hex2ascii>
Bookmarks