Hex File Format Study notes

Source: Internet
Author: User

This is also a study excerpt: Original address: http://blog.csdn.net/syrchina/article/details/7004998

in order to write a can according to their own requirements of the ISP program, probably studied the hex file format. Write your study notes and think that you can re-solidify what you learn.

Hex file in the behavior unit. Each line begins with the character ': ' (0x3a), with the carriage return line break 0x0d, 0x0a to the end. All content between the start and end of each line is represented as a character. For example, if the data is 0x1A, then the conversion to the hex format line is 0x31 0x41. If the data is 16bit, such as an address, the upper is displayed first, and the bottom bit is displayed. For example 0x1234, converted to hex format file into 0x31 0x32 0x33 0x34, after the show is 1234. Consider the data portion of the content as a hex data for every 2 characters, for example:

: 020000040000FA, I think of it as 0x02 0x00 0x00 0x04 0x00 0x00 0xFA

The first 0x02 is a data length.

Followed by 0x00 0x00 as the address.

Then the following 0x04 is the data type, the types are divided into several categories:

' xx ' Data Record

'% ' End of File Record

' Extended ' Segment Address Record

' Segment ' Start Address Record

' Extended ' Linear Address Record

' On ' Start Linear Address Record

Then, then, the two 0x00 0x00 after 0x04 is the data. The last 0xFA is a check code.

Each line of the hex file is in this format:

<0x3a>

[Data length 1Byte]

[Data address 2Byte]

[Data type 1Byte]

[Data Nbyte]

[Check 1Byte]

<0x0d>

<0x0a>

In the example:

: 1000000018f09fe518f09fe518f09fe518f09fe5c0

The data row format above the installation is analyzed as follows:

<0x3a>

[Data length 1Byte]

10

[Data address 2Byte]

00 00

[Data type 1Byte]

00

[Data Nbyte]

18f09fe518f09fe518f09fe518f09fe5

[Check 1Byte]

C0

<0x0d>

<0x0a>

The data in each row is not certain, the second direct data length is 0, then there is no data for this row.

Because each row identifies only 2Byte of data address, so the maximum is only 64K, in order to save the larger data address data, there is the extended linearaddress Record. If the data type of this row is 0x04, then the data in this row is the base address of the subsequent data. For example:

: 020000040004f6

: 1000000018f09fe518f09fe518f09fe518f09fe5c0

: 1000100018f09fe5805f20b9f0ff1fe518f09fe51d

The first line, is the extended linearaddress record, the base address is 0x0004, the second row is the data Record, the address value is 0x0000. Then the data 18f09fe518f09fe518f09fe518f09fe5 to be written to flash in the address is (0x0004 << 16) | 0x0000, which is the address of 0x40000 written to Flash. Similarly, the third row of data is written to the address 0x40010. When the data for a hex file exceeds 64k, multiple extended Linear Address Record will appear in the file.

The end of file Record line is the last line of each hex file. For example:

: 00000001FF

Such a row of data content is fixed, the data length is 0, the address is 0.

Checksum value: The last value of each row is the checksum of this row of data. For example:

: 1000000018f09fe518f09fe518f09fe518f09fe5c0 in this line of 0xC0

: 1000100018f09fe5805f20b9f0ff1fe518f09fe51d in this line of 0x1D

The algorithm for the checksum is: calculates the remainder of all the bytes and modulo 256 after the 0x3a (excluding 0x3a). That is, the byte binary arithmetic sum, excluding more than 256 of the overflow value, and then subtract this arithmetic sum with 0x100, the obtained value is the checksum of this line.

And then post a "self" code for the checksum:

void cbp100dlg::onbnclickedbtnoffsetbp100 ()
{
TODO: Add control notification Handler code here
Char test[] = "1000000018f09fe518f09fe518f09fe518f09fe5"; The data here comes from an example given in the above article, removing the final checksum
Char nbytes,type,val,cksum;
Char data[100];
unsigned int addr;
char *s = test; s point to test

if (sscanf (S, "%02x%04x%02x", &nbytes,&addr,&type)! = 3)//The next 8 bytes are data size, address and data type
{
return;
}

Cksum = nbytes + addr + (addr>>8) + type;
char line_cksum = 0;

Char i=0;
S=s+8; The pointer pushes backwards 8
for (i=0;i<nbytes;i++)
{
val = 0;
if (sscanf (S, "%02x", &val)! = 1)
{
return;
}

s+=2; Character push backwards 2 bits
Cksum+=val;
Data[i] = val;
}

Line_cksum = 0x100-cksum; This code validates the checksum given above

TRACE ("cksum =%d", cksum);
}

Hex File Format Study notes

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.