Intel HEX

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Intel HEX is a file format that conveys binary information in ASCII text form. It is commonly used for programming microcontrollers, EPROMs, and other types of programmable logic devices. In a typical application, a compiler or assembler converts a program's source code (such as in C or assembly language) to machine code and outputs it into a HEX file. The HEX file is then imported by a programmer to "burn" the machine code into a ROM, or is transferred to the target system for loading and execution.[1]

Format[edit]

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. Each text line is called a record.

Record structure[edit]

A record (line of text) consists of six fields (parts) that appear in order from left to right:

  1. Start code, one character, an ASCII colon ':'.
  2. Byte count, two hex digits, indicating the number of bytes (hex digit pairs) in the data field. The maximum byte count is 255 (0xFF). 16 (0x10) and 32 (0x20) are commonly used byte counts.
  3. Address, four hex digits, representing 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, thus allowing 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.
  4. Record type (see record types below), two hex digits, 00 to 05, defining the meaning of the data field.
  5. Data, a sequence of n bytes of data, represented by 2n hex digits. Some records omit this field (n equals zero). The meaning and interpretation of data bytes depends on the application.
  6. Checksum, two hex digits, a computed value that can be used to verify the record has no errors.

Color legend[edit]

As a visual aid, the fields of Intel HEX records are colored throughout this article as follows:

  Start code   Byte count   Address   Record type   Data   Checksum

Checksum calculation[edit]

The checksum value is the least significant byte (LSB) of the two's complement of the sum of the values of all fields, excluding the start code and checksum. It is computed by summing the hex-encoded byte values (hex digit pairs), extracting the LSB of the sum, and calculating the 2's complement of the LSB (e.g., by inverting it and adding 0x01).

For example, in the case of the record :0300300002337A1E, the sum of the hex digit pairs is 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2. The two's complement of E2 is 1E, which is the checksum appearing at the end of the record.

The validity of a record can be checked by computing its checksum and verifying that the computed checksum equals the checksum appearing in the record. An error is indicated if the checksums differ.

Text line terminators[edit]

Intel HEX records are separated by one or more ASCII line termination characters so that each record appears alone on a text line. This enhances legibility by visually delimiting the records and it also provides padding between records that can be used to improve machine parsing efficiency.

Programs that create HEX records typically use line termination characters that conform to the conventions of their operating systems. For example, Linux programs use a single LF (line feed, hex value 0A) character to terminate lines, whereas Windows programs use a CR (carriage return, hex value 0D) followed by a LF.

Record types[edit]

Intel HEX has six standard record types:

Hex code Record type Description Example
00 Data Contains data and a 16-bit starting address for the data. The byte count specifies number of data bytes in the record. :0B0010006164647265737320676170A7
01 End Of File Must occur exactly once per file in the last line of the file. The data field is empty (thus byte count is 00) and the address field is typically 0000. :00000001FF
02 Extended Segment Address The data field contains a 16-bit segment base address (thus byte count is 02) and the address field is not used (must be 0000). Used when 16-bit addresses are too small, equivalent to 80x86 real mode addressing. The segment address from the most recent 02 record is multiplied by 16 and added to each subsequent data record address to form the physical starting address for the data. This allows addressing up to one megabyte of address space. :020000021200EA
03 Start Segment Address For 80x86 processors, specifies the initial content of the CS:IP registers. The address field is 0000, the byte count is 04, the first two bytes are the CS value, the latter two are the IP value. :0400000300003800C1
04 Extended Linear Address Allows for 32 bit addressing (up to 4GiB). The address field is 0000 (not meaningful) and the byte count is 02. The two data bytes (comprising four hex digits in big endian order) specify the upper 16 bits of the 32 bit absolute address for all subsequent type 00 records; these upper 16 address bits apply until the next type 04 record. If no type 04 record has preceded a type 00 record, the upper 16 address bits default to 0000. The absolute address for a type 00 record is formed by combining the upper 16 address bits of the most recent type 04 record with the low 16 address bits of the type 00 record. :02000004FFFFFC
05 Start Linear Address The address field is 0000, the byte count is 04. The 4 data bytes represent the 32-bit value loaded into the EIP register of the 80386 and higher CPU. :04000005000000CD2A

Proprietary record types are also used in some applications. For example:

  • 20, ROM code, used by Samsung SAMA assembler[2][3]
  • 22, Extension code, used by Samsung Smart Studio microcontroller development system

Special format names are sometimes used to denote HEX files that employ specific subsets of record types. For example, "I8HEX" files use only record types 00/01 (16 bit addresses), "I16HEX" uses types 00-03 (20 bit addresses), and "I32HEX" uses types 00/01/04/05 (32 bit addresses).

File example[edit]

:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF 

See also[edit]

References[edit]

Further reading[edit]

External links[edit]

Documentation
Software
  • SRecord, multi-platform GPL'ed tool for manipulating EPROM load files.
  • Intel hex to bin converter with source code, written in C, Source code of a program that allocates memory dynamically and is able to generate all the .bin files needed by the .hex file, respecting the maximum given size.
  • Binex, a converter between Intel HEX and binary.
  • libgis, open source library to handle Intel HEX (and more).