www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com

The HEX file format

Most compilers for microcontrollers produce a HEX file after successful compilation of a program. This is an ASCII file that gives the bytes of machine code. An example will make things clear.

Example: Suppose that we want to download the bytes 0x34, 0xdf and 0x12 starting from location 0x2245. Then the HEX file will have the line:

:0322450034DF12cc

Let us learn to make sense of this line. Each line in a HEX file starts with a : (colon). This makes life easier for a program that reads a HEX file. Relying merely on the newline character at the end of a line may cause trouble as different platforms use different end-of-line characters. Now split the remaining characters in the line as follows:

All numbers are in hexadecimal. The first 2 letters (exluding the colon) give you a number (3 in our example). So this line is going to specify 3 bytes of machine code. The next 4 letters give the starting address (0x2245 here). The next letters give the type. It can be either 00 or 01 or 03. 00 means code line, 01 means last line (no code in it), 03 means a comment line.

Extracting the code bytes from a HEX file is a simple matter using C. The following lines should be enough guideline.

sscanf(line+1,"%2x%4x%2x",&nByte,&addr,&type);
if(type==0) {
  for(i=0,ptr=line+1+2+4+2;i<nByte;i++,ptr += 2)
    sscanf(ptr,"%2x",&;codeByte[i]);
}


Prev
© Arnab Chakraborty (2008)