HEX file format

Source: Internet
Author: User

HEX file format learning notes

In order to write a program that can perform ISP according to your own requirements, I learned about the Hex file format. Write the study notes to consolidate the learning content.

The Hex file is in the unit of action. Each line starts with the character ':' (0x3a) and ends with the line break 0x0d and 0x0a. All content between the start and end of each line is represented in characters. For example, if the data is 0x1a, the rows converted to hex format are 0x31 0x41. If the data is 16 bits, for example, the address is displayed as a high position, and then the base position. For example, convert 0x1234 to a hex format file and convert it to 0x31 0x32 0x33 0x34. After the file is displayed, It is 1234. The data part is treated as a hex data every two characters, for example:

: 020000040000fa. I think of it as 0x02 0x00 0x00 0x04 0x00 0x00 0xfa.

The first 0x02 is the data length.

Followed by the 0x00 0x00 address.

The following 0x04 is the data type, which has the following types:

'00' data record

'01' end of file record

'02 'extended segment address record

'03 'Start segment address record

'04 'Extended Linear Address Record

'05 'Start linear Address Record

Then, the two 0x00 0x00 values after 0x04 are the data. The last 0xfa is the verification code.

Each row of the Hex file is in the following format:


[Data Length: 1 byte]

[Data address 2 byte]

[Data Type 1 byte]

[Data nbyte]

[Verify 1 byte]




For example:

: 000000018f09fe518f09fe518f09fe518f09fe5c0

Install the preceding data row format analysis as follows:



[Data Length: 1 byte]


[Data address 2 byte]

00 00

[Data Type 1 byte]


[Data nbyte]


[Verify 1 byte]







The data in each row is not necessarily there, and the second direct data length is 0, so there is no data in this line.

Because each row identifies only 2 bytes of data addresses, the maximum value is 64 KB. Extended linearaddress record is available to store larger data addresses. If the data type of this row is 0x04, the data in this row is the base address of the subsequent data. For example:

: 020000040004f6

: 000000018f09fe518f09fe518f09fe518f09fe5c0

: 1000100018f09fe5805f20b9f0ff1fe518f09fe51d

The first line is extended linearaddress record, where the base address is 0x0004, and the second line is data record, where the address value is 0x0000. Therefore, if the data 18f09fe518f09fe518f09fe5 is to be written to flash, the address is (0x0004 <16) | 0x0000, that is, the address 0x40000 written to flash. Similarly, the data write address of the third row is 0x40010. When the data in a HEX file exceeds 64 KB, multiple Extended Linear
Address Record.

The end of file record row is the last row of each HEX file. For example:

: 00000001ff

The data content of such a row is fixed. The data length is 0 and the address is 0.


Check value: the last value of each row is the checksum of the data in this row. For example:

: 0xc0 in this line of 00018f09fe518f09fe518f09fe5c0

: 1000100018f09fe5805f20b9f0ff1fe518f09fe51d 0x1d in this line


The Checksum algorithm is used to calculate the sum of all bytes after 0x3a (excluding 0x3a) and the remainder of the modulo 256. That is, the binary arithmetic sum of each byte, excluding the overflow value of more than 256, is calculated by subtracting 0x100 from this arithmetic sum.


Byyifan 2006.3.16


What is Intel hex format?


An Intel HEX file is an ASCII text file that records text lines. In an Intel HEX file, each line is a hex record.
A machine code or data constant consisting of hexadecimal numbers. Intel hex files are often used to transmit programs or data.
Stored in Rom. EPROM. Most programmers and simulators use intel hex files.

Record format

An Intel HEX file can contain any number of hexadecimal records. Each record has five fields. The following is a record format.

: Llaaaatt [DD...] CC

Each group of letters is an independent domain. Each letter is a hexadecimal number. Each domain consists of at least two hexadecimal numbers. The following is a byte description.

: Colon is the start of each intel hex record

Ll is the length field of this record, which indicates the number of bytes of data (dd.

Aaaa is the address field, which indicates the starting address of the data.
<If it is a data record, this indicates the offset address of the data in the record to be burned in the EPROM,
For those that do not support extended segment addresses and extended linear addresses, such as 89C51, this is the starting address of this record>

TT indicates the type of the hex record. It may be the following types:
00 ---- data record
01 ---- end of File
02 ---- extended segment address record
04 ---- Extended Linear address records

Dd is a data field that represents one byte of data. A record may contain multiple data bytes, and the number of bytes is acceptable.
View description of LL domain

CC is the effect and domain, indicating the effect and calculation of the record. The calculation method is to associate all the letter pairs starting with the colon

<Do not include the hexadecimal number indicated by this verification word and colon>
<A pair of letters indicates a hexadecimal number. Such a hexadecimal number is a byte.>

All add up and then divide the remainder obtained by MoD 256. The final complement code for finding the remainder is the effective byte CC.
: 0300000002005e9d
Cc = 0x01 + not (0x03 + 0x00 + 0x00 + 0x00 + 0x02 + 0x00 + 0x5e) % 0x100) = 0x01 + 0x9c = 0x9d

C language description:
Uchar cc;
Cc = (uchar )~ (0x03 + 0x00 + 0x00 + 0x00 + 0x02 + 0x00 + 0x5e );
CC ++;
Data Records

An intelhex file consists of several data records. A Data Record ends with a carriage return and a line feed.
<Press enter to 0x0d for behavior change 0x0a>

For example, the following data record
: 10246200464c5549442050524f46494c4500464c33

10 is the number of bytes of data recorded in this row.
2462 is the starting address of the data in the memory <the EPROM address to be written>
00 is the record Type 00 (a data record)
464c to 464c is Data
33 is the validation and

Extended Linear Address Record (hex386)
Extended Linear address records are also known as 32-bit address records and hex386 records, which contain 16-31 (16-31) high-Bit Data addresses. Such Extended Linear records always have two bytes of data, as shown below:

: 02000004 fffffc
02 is the number of data bytes recorded
0000 is the address field, which is always 0000 in the extended address record.
04 is record type 04 (Extended Address Record)
FFFF is a 16-bit high address.
The FC is the record verification sum. The calculation method is as follows:
01 H + not (02 H + 00 H + 00 H + 04 H + FFH)

When an Extended Linear address record is read, the data area of the Extended Linear address record is saved.
And applied to the records read from Intel hex files. This Extended Linear record is always valid,
Until you read The Next Extended Linear record.

Absolute Memory Address = address in the data record + expanded linear address after shift

The following is an example of this process.

Get address 2462 from the address field of the data record
Obtain the address FFFF from the address field of the Extended Linear address record.
Absolute Memory Address ffff2462

Extended segment address record (hex86)

The extended segment address record is also known as the hex86 record, which contains 4-19 data address segments,
This extended segment address record always has two bytes of data, as shown below:

: 020000021200ea
02 is the number of data bytes in the record
0000 is the address field. In the extended segment address record, this field is always 0000
02 is record type 02 (Extended segment address ID)
1200 is the address of the segment
EA is a validation and
The calculation is as follows:
01 H + not (02 H + 00 H + 00 H + 02 H + 12 h + 00 H ).

When the extended segment address record is read, the extended segment address will be stored and applied to the record that will be read from the Intel HEX file later. This segment address remains valid until it reads the next extended segment address record.

Absolute Memory Address = address in the data record + extended segment address after shift

The address field in the extended segment address record after the address field in the data record is moved

The following is an example of this process.

Obtain Address 2 4 6 2 from the address field of the data record
Obtain Address 1 2 0 0 from the address field of the extended segment address record
Absolute Memory Address 0 0 0 1 4 4 6 2

End of file (EOF)
An Intel HEX file must have a file end record. The type field of this record must be 01,
An EOF record always looks like this:
: 00000001ff
00 indicates the number of data bytes in the record.
0000 this address is meaningless for the EOF record
01 The record type is 01 (End record mark)
FF is the verification and calculation as follows
01 H + not (00 H + 00 H + 00 H + 01 H ).


: Bbaaaatthhhh... hhhhcc

BB: byte
Aaaa: the starting address of the data record. The highest position is in the back
Because this format only supports 8 bits, the address is multiplied
Therefore, in order to obtain the actual PIC address, we need to divide the address by 2
TT: Type
00 data records
01 end of record
04 extended Address Record (indicating the 32-bit address prefix, of course, this can only be in inhx32)
Hhhh: a data record of a word, with a high byte in front and a low byte in the back
After TT, there are a total of BB/2 Characters of data
Cc: the checksum of a byte

Because pic16f873a only has 4 K program space

Therefore, there will be no linear address record for TT = 04


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.