Full name: Intel hexadecimal object file format.
Definition
Intel HEX is a text file format for a memory image: a set of bytes with the memory addresses where they belong. Each line is one record. A record begins with a colon and is written in hexadecimal (base 16) digits, two per byte. It carries a byte count, a 16-bit address, a record type, the data bytes and one checksum §4.32 byte against copying errors. Tools that write programs into memory chips and into microcontrollers §6.1 (single-chip computers) read this format. Intel published it under the name hexadecimal object file format.
The idea
A raw binary file is only a row of bytes. It does not say where in memory the bytes belong. It also cannot leave a gap: if a program occupies two areas that are far apart, a raw file must fill the space between them with padding. Firmware, the program stored in a device, often has exactly that shape, for example code at one address and a small block of settings millions of bytes higher.
Intel HEX solves both problems by storing the image as a list of labeled packets. Each packet says: these bytes go at this address. The packets are printable text, which, as the specification points out, can be stored on paper tape and punched cards and shown on terminals and printers. Because every packet carries its own address, the same image can be cut into packets in many ways. That is why two different files can describe the same memory contents.
How it works
A record has six fields, written one after another without spaces. The names in parentheses are those of the specification.
:LLAAAATTDD...DDCC
:the record mark.LLthe number of data bytes in the record (RECLEN), from 0 to 255.AAAAa 16-bit address, most significant byte first (LOAD OFFSET).TTthe record type (RECTYP).DDthe data bytes.CCthe checksum (CHKSUM).
The specification defines six record types.
00, data: bytes of the image.01, end of file: the last record.02, extended segment address: a base for the 20-bit addresses of Intel’s 16-bit processors.03, start segment address: where execution begins, in that 20-bit form.04, extended linear address: the upper 16 bits of a 32-bit address.05, start linear address: where execution begins, as a 32-bit address.
A reader turns the records of a file with 32-bit addresses into memory contents as follows.
Start with the upper address bits .
Read a record and convert each pair of digits to a byte. Add all bytes of the record, including the checksum. If the low 8 bits of the total are not zero, the record is damaged.
For a type
04record, set to its two data bytes, read as one 16-bit number with the more significant byte first.For a type
00record with address field , place data byte number , counting from 0, at address . Here is the remainder when is divided by , which keeps the address within 32 bits.Repeat from step 2 with the next record, and stop at the type
01record.
A writer computes the checksum so that step 2 succeeds. It adds the bytes from LL through the last data byte, keeps the low 8 bits of the total, and stores the value that brings those 8 bits to zero.
Worked example
File A has three records:
:020000040800F2
:0400100001020304E2
:00000001FF
First record. 02 data bytes, address field 0000, type 04, data 08 00, checksum F2. It sets the upper 16 address bits to 0x0800, where the prefix 0x marks a hexadecimal number, so the data that follows lies at 0x08000000 and above. Checksum: and , which is F2 in hexadecimal.
Second record. 04 data bytes, address field 0010, type 00, data 01 02 03 04, checksum E2. The first data byte goes to address 0x08000010, and the other three follow it. Checksum: the address byte 10 counts as 16, so and , which is E2. The reader’s check is , whose low 8 bits are zero.
Third record. End of file: and , which is FF.
The memory image of file A is four bytes:
| address | 0x08000010 |
0x08000011 |
0x08000012 |
0x08000013 |
|---|---|---|---|---|
| value | 01 |
02 |
03 |
04 |
File B has four records:
:020000040800F2
:020012000304E5
:020010000102EB
:00000001FF
Its second record puts 03 04 at 0x08000012. Checksum: and , which is E5. Its third record puts 01 02 at 0x08000010. Checksum: and , which is EB.
File B describes the same four addresses with the same four values. The two files differ as text: A has 3 lines and B has 4, and a comparison of the files byte by byte reports a difference. As memory images they are equal.
Stated precisely
The meaning of an Intel HEX file is a memory image: a set of pairs of an address and a byte value, with at most one value for each address, plus an optional start address. Two files encode the same memory contents exactly when they define the same set of pairs.
A record is valid when the sum of all its bytes, from the byte count through the checksum, leaves the remainder 0 on division by 256. Let be the sum of the bytes before the checksum. The checksum is .
For a data record in a file with 32-bit addresses, let be the 16-bit value of the most recent type 04 record, or 0 if there was none. Let be the address field of the data record, and let be the position of a data byte in it, counting from 0. The byte belongs at address . In the older 20-bit form, a type 02 record gives a 16-bit value , and the byte belongs at address .
The following changes to a file leave its memory image the same:
cutting the data into records of a different length;
putting the data records in a different order, provided that each record stays under the same upper address bits and no address receives two different values;
repeating a type
04record that restates the upper bits already in effect;adding or removing a record that gives a start address, which changes the start address and no memory contents.
The specification does not say what a reader must do when two data records give different values for one address. Such a file has no single agreed meaning.
Limits and common mistakes
The checksum detects accidental damage and little else. It is a plain sum, so it catches any single wrong byte, but two errors can cancel. The record :0400100001020403E2, in which the last two data bytes are swapped, passes the check. The checksum gives no protection against deliberate change, because anyone who edits the data can compute a new checksum. That protection needs a cryptographic check over the image, such as a message authentication code §5.7 or a digital signature §5.14, values that only the holder of a secret key can compute.
Addresses that no record mentions are not part of the image. The file does not say that they are zero or erased. A tool that converts the file to a raw binary file has to invent filler bytes for the gaps.
Three claims must be kept apart: the two files are equal as text; the two files encode the same memory image; the device now holds that image. Equal images do not mean equal programming: the file does not say which memory a programming tool erases first, and a loader, the tool that puts the image into memory, may choose its own start address when the file gives none. The third claim needs the contents to be read back from the device.
The address field has only 16 bits. An image above the first 64 KiB needs records of type 04 or 02, and a reader that ignores them places the data at wrong addresses.
The specification defines what a record contains. It does not define what separates one record from the next, so files also differ in their line endings.
Questions answered
Can two textual HEX files encode the same addressed memory contents?
Yes, two Intel HEX files that differ as text can encode the same addressed memory contents. An Intel HEX (hexadecimal) file is a text encoding of a memory image, meaning a set of byte values with their memory addresses, and the encoding is not unique. The file is a list of records. Each data record carries the low 16 bits of its own address, and a separate kind of record sets the upper 16 bits for the data records that follow it. The same bytes can therefore be cut into records of different lengths and, under the same upper address bits, written in a different order. Records that set the upper address bits can be repeated. A record that gives a start address can be present or absent. All of these change the text, and none changes which byte belongs at which address.
To decide whether two files encode the same contents, do not compare the text, and do not compare a hash (a fingerprint computed from the bytes) of the text. Read each file into a table from address to byte value, checking the checksum of every record, and compare the two tables. Decide beforehand how to treat three things that the table does not capture or that the format leaves open: addresses that neither file mentions, an address that one file sets twice with different values, and the start address.
Equal tables show that the files describe the same memory contents. They do not show that a device was programmed with them, or that the programming succeeded.
Related entries: ELF (§12.6), Integrity (§1.9), Overflow-safe range check (§4.17), Checksum versus CRC (§4.32), Flash memory (§6.6). Further reading: [112].