MPLAB® IPE Intel HEX File Format

Last modified by Microchip on 2023/11/09 09:10

HEX File Format

The Serialized Quick Turn Programming (SQTP℠) file records (lines of text) conform to Intel® HEX file format. Intel HEX consists of lines of ASCII text that are separated by line feed or carriage return characters, or both. Each text line contains hexadecimal characters that encode multiple binary numbers. The binary numbers may represent data, memory addresses, or other values, depending on their position in the line, and the type and length of the line.

This section, Intel HEX File Format, uses material from the Wikipedia article "Intel HEX", which is released under the Creative Commons Attribution-ShareAlike 3.0 Unported License (view authors).

File Records

A record is a line of text that consists of six fields. Each field contains a character and multiple digits in the following order, from left to right:

Field 1.Start Code – one character
This character is an ASCII colon (:).
Field 2.Byte Count – two hexadecimal digits
These two digits indicate the number of bytes (HEX digit pairs) in the data field. The maximum byte count is 255 (0xFF).
For example, 16 (0x10) and 32 (0x20) byte counts are commonly used.
Field 3.Address – four hexadecimal digits
These four digits represent the 16-bit beginning memory address offset of the data.
The physical address of the data is computed by adding this offset to a previously established base address. This provides memory addressing beyond the 64-kilobyte limit of 16-bit addresses. The base address, which defaults to zero, can be changed by various types of records.
Base addresses and address offsets are always expressed as big-endian values.
Field 4.Record Type – two hexadecimal digits, 00 to 05
These two digits define the meaning of the data field. For SQTP, only three types are used; 00, 01, and 04 (see the table in Example SQTP File).
Field 5.Data – a sequence of n bytes of data
Data is represented by 2n hexadecimal digits. Some records omit this field (n equals zero).
The meaning and interpretation of the data bytes depend on the application.
Field 6.Checksum – two hexadecimal digits
These two digits represent a computed value that is used to verify the record has no errors by ensuring the summation of each of the bytes in the line adds up to zero. See the Calculating the Checksum section.

Calculating the Checksum

As mentioned in the File Records format table, the last two characters represent a checksum of the data in the line. Since the checksum is a two-digit hexadecimal value, it may represent a value of 0 to 255, inclusive.

The checksum is calculated by summing the value of the data on the line, excluding the leading colon and checksum byte itself, and taking its two's complement. For example, consider the line:

Breaking this line into its components:

  • Record Length: 03 (3 bytes of data)
  • Address: 0030 (the 3 bytes will be stored at 0030, 0031, and 0032)
  • Record Type: 00 (normal data)
  • Data: 02, 33, 7A
  • Checksum: 1E

Taking all the data bytes above, we have to calculate the checksum based on the following hexadecimal values:

03 + 00 + 30 + 00 + 02 + 33 + 7A = E2

The two's complement of E2 is 1E which is, as you can see, the checksum value. The two's complement of a number is the value that must be added to the number to reach the value 256 (decimal). That is to say, E2 + 1E = 100.

You may also calculate the two's complement by subtracting the value from 100h. In other words, 100h - E2h = 1Eh, which is the checksum.

If the value in question is greater than FFh, simply take the part which is less than 100h.

For example, if you want the two's complement of the value 494h, simply drop the leading 4 which leaves you with 94h. The two's complement of 94h is 6Ch.