src/util/intelhex.js
function extractDataFromIntelHexLine(intelHexLine) {
  if (!intelHexLine.startsWith(':')) {
    throw new Error(`Intel hex lines need to start with ':'`)
  }

  let asciiHex = intelHexLine.slice(1, intelHexLine.length)

  if (asciiHex.length === 0) {
    throw new Error(`Length of ascii hex string needs to be greater than 0`)
  }

  if (asciiHex.length % 2 !== 0) {
    throw new Error(`Length of ascii hex string needs to be even`)
  }

  let rawBytes = []

  for (let i = 0; i < asciiHex.length; i += 2) {
    let asciiByte = asciiHex.slice(i, i + 2)
    rawBytes.push(parseInt(asciiByte, 16))
  }

  if (rawBytes[3] === INTEL_HEX_DATA) {
    let dataBytes = rawBytes.slice(4, rawBytes.length - 1)
    let bytes = new buffer.Buffer(dataBytes.length)
    for (let i = 0; i < dataBytes.length; i++) {
      bytes.writeUInt8(dataBytes[i], i)
    }
    return bytes
  } else {
    return null
  }

}
function extractDataFromIntelHexLine(intelHexLine) {
  if (!intelHexLine.startsWith(':')) {
    throw new Error(`Intel hex lines need to start with ':'`)
  }

  let asciiHex = intelHexLine.slice(1, intelHexLine.length)

  if (asciiHex.length === 0) {
    throw new Error(`Length of ascii hex string needs to be greater than 0`)
  }

  if (asciiHex.length % 2 !== 0) {
    throw new Error(`Length of ascii hex string needs to be even`)
  }

  let rawBytes = []

  for (let i = 0; i < asciiHex.length; i += 2) {
    let asciiByte = asciiHex.slice(i, i + 2)
    rawBytes.push(parseInt(asciiByte, 16))
  }

  if (rawBytes[3] === INTEL_HEX_DATA) {
    let dataBytes = rawBytes.slice(4, rawBytes.length - 1)
    let bytes = new buffer.Buffer(dataBytes.length)
    for (let i = 0; i < dataBytes.length; i++) {
      bytes.writeUInt8(dataBytes[i], i)
    }
    return bytes
  } else {
    return null
  }

}
function extractDataFromIntelHexLine ( intelHexLine ) { if ( ! intelHexLine . startsWith ( ':' ) ) { throw new Error ( ` ` ) } let asciiHex = intelHexLine . slice ( 1 , intelHexLine . length ) if ( asciiHex . length === 0 ) { throw new Error ( ` ` ) } if ( asciiHex . length % 2 !== 0 ) { throw new Error ( ` ` ) } let rawBytes = [ ] for ( let i = 0 ; i < asciiHex . length ; i += 2 ) { let asciiByte = asciiHex . slice ( i , i + 2 ) rawBytes . push ( parseInt ( asciiByte , 16 ) ) } if ( rawBytes [ 3 ] === INTEL_HEX_DATA ) { let dataBytes = rawBytes . slice ( 4 , rawBytes . length - 1 ) let bytes = new buffer . Buffer ( dataBytes . length ) for ( let i = 0 ; i < dataBytes . length ; i ++ ) { bytes . writeUInt8 ( dataBytes [ i ] , i ) } return bytes } else { return null } }
Extract the "data" from an Intel hex line

An Intel hex line looks like this

:100000005CC000000C9450080C947D087EC0000079

Once the ':' character is removed, each set of two characters represent a
Hexadecimal octet, or byte. However, not every byte in the string is considered
data, here is what the bytes mean:

Byte(s) [  0]    = Byte count
Byte(s) [1:2]    = Address
Byte(s) [  3]    = Record type
Byte(s) [  n]    = Data
Byte(s) [n+1]    = Checksum

@param intelHexLine Ascii Intel hex line
@returns
Extract the data from an Intel hex line