• Please review our updated Terms and Rules here

Utility to convert intel hex into ascii stream for sending from terminal emulator

candrews

Experienced Member
Joined
Jan 29, 2016
Messages
80
Location
WA State, USA
I often have the need to simulate a paper tape reader and send ascii from a terminal to some hardware using “send file” as if typing (in hex or octal). For example, send code to a little boot loader or resident monitor or data to an eprom programmer

Before I start yet another project, does anyone have a utility that:
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
Ideally breaks the output into multiple files of a target size or at original address breaks

I am sure something close to this must already exist but I can’t find it online from the usual suspects

Thanks

Craig
 
If you had Win32Forth on you machine I could write the code in about 20 to 30 minutes. I have similar code that read Hex files an writes out a binary file. It wouldn't that a lot to modify it to write out octal ascii.
Dwight
 
If you had Win32Forth on you machine I could write the code in about 20 to 30 minutes. I have similar code that read Hex files an writes out a binary file. It wouldn't that a lot to modify it to write out octal ascii.
Dwight

Thanks Dwight. But if something doesn’t crop up I’ll do it in C

Craig
 
I often have the need to simulate a paper tape reader and send ascii from a terminal to some hardware using “send file” as if typing (in hex or octal). For example, send code to a little boot loader or resident monitor or data to an eprom programmer

Before I start yet another project, does anyone have a utility that:
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
Ideally breaks the output into multiple files of a target size or at original address breaks

I am sure something close to this must already exist but I can’t find it online from the usual suspects

Thanks

Craig

You could use a device programmer's utility to read in the Hex file and write it out as a Bin file, that ought to make it easier to separate out for creation of a stream. But I'm not sure that's what you're after.

I use a Needhams EMP-10 with this software:
http://ps-2.kev009.com/ohlandl/Needham_EMP20/EMP-20_Programmer.html
 
You could use a device programmer's utility to read in the Hex file and write it out as a Bin file, that ought to make it easier to separate out for creation of a stream. But I'm not sure that's what you're after.

I use a Needhams EMP-10 with this software:
http://ps-2.kev009.com/ohlandl/Needham_EMP20/EMP-20_Programmer.html

His application expects an octal stream, not binary or S format or Intel Hex.
One could make a "blue pill" be a translator. It would be trivial code but would require a little circuit to take the output to 20ma loop.
Dwight
 
This could also be written in Python, which is available (free) for all sorts of platforms including for Windows and for Linux, including the Raspberry Pi. It might be useful to know what OS the OP would be wanting to run this utility on - Windows / PC assumed, but perhaps not?
 
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>
 
Last edited:
On a Unix/Linux machine one could do

objcopy -I ihex -O binary file.hex file.bin && cat file.bin | od -v -b -w8 -A none

Unfortunately, objcopy can't output to stdout, so it can't be "piped". Also od makes a new line every 8 chars (in this example, but can have -w whatever you like).

Frank
 
Back
Top