code: def hex2bin(fin, fout, start=None, end=None, size=None, pad=None):
      
      try:
          h = IntelHex(fin)
      except HexReaderError:
          e = sys.exc_info()[1]     # current exception
          txt = "ERROR: bad HEX file: %s" % str(e)
          print(txt)
          return 1
  
      # start, end, size
      if size != None and size != 0:
          if end == None:
              if start == None:
                  start = h.minaddr()
              end = start + size - 1
          else:
              if (end+1) >= size:
                  start = end + 1 - size
              else:
                  start = 0
  
      try:
          if pad is not None:
              # using .padding attribute rather than pad argument to function call
              h.padding = pad
          h.tobinfile(fout, start, end)
      except IOError:
          e = sys.exc_info()[1]     # current exception
          txt = "ERROR: Could not write to file: %s: %s" % (fout, str(e))
          print(txt)
          return 1
  
      return 0
docs: Hex-to-Bin convertor engine.
      @return     0   if all OK
  
      @param  fin     input hex file (filename or file-like object)
      @param  fout    output bin file (filename or file-like object)
      @param  start   start of address range (optional)
      @param  end     end of address range (inclusive; optional)
      @param  size    size of resulting file (in bytes) (optional)
      @param  pad     padding byte (optional)