Appendix B: HEX File Format

Flash programmers process the standard hexadecimal (hex) format used by the Microchip development tools. The format supported is the Intel® HEX32 format (INHX32). Refer to Section 1.75 “Hex file Formats” in the MPASM Assembler, MPLINK Object Linker, MPLIB Object Librarian User’s Guide (DS33014).

The basic format of the hex file is:
  • :BBAAAATTHHHH...HHHHCC
Each data record begins with a nine character prefix and always ends with a two character checksum. All records begin with :, regardless of the format. The individual elements are described below.
  • BB: It is a two-digit hexadecimal byte count representing the number of data bytes that appear on the line. Divide this number by two to get the number of words per line.
  • AAAA: It is a four-digit hexadecimal address representing the starting address of the data record. Format is high byte first followed by low byte.
  • TT: It is a two-digit record type.
    • 00 for data records
    • 01 for end-of-file records
    • 04 for extended-address record
  • HHHH: It is a four-digit hexadecimal data word. Format is low byte followed by high byte. There will be BB/2 data words following TT.
  • CC: It is a two-digit hexadecimal checksum that is the 2’s complement of the sum of all the preceding bytes in the line record.

Because the Intel® hex file format is byte-oriented but the 16-bit program counter is not, program memory sections require special treatment. Each 24-bit program word is extended to 32 bits by inserting a so called “phantom byte”. Each program memory address is multiplied by 2 to yield a byte address.

As an example, a section that is located at 0x100 in program memory will be represented in the hex file as 0x200.

The hex file will be produced with the following contents:
  • :020000040000fa
  • :040200003322110096
  • :00000001FF

The data record (second line) has a load address of 0200, while the source code specified address is 0x100. The data is represented in Little Endian (LE) format, that is the LSB appears first and the phantom byte appears last, before the checksum.