BIN2HEX

Utility to convert a Binary file to an Intel Hex file
 

BIN2HEX is a simple utility which converts a binary file to an Intel HEX file. There are lots of these converter on web, however, I couldn't find one that also generated an execute record. This is useful if you want to use the format for bootstrap loaders.

Usage

BIN2HEX <input_binary_filename> <output_hex_filename>  {-o offset} {-s segment} {-e execute}

Where Optional:

offset       Output file address offset in hex.
segment   Extended address record offset in hex (e.g. Segment address on a 8086).
execute    Execute address offset in hex (e.g. IP address on a 8086).

The Intel Hex format is quite simple and consist of 6 fields:

Start Char :

Length(1)

Address(2)

Rec Type(1)

Data(n)

Checksum(1)

A record always start with the ':' character followed by a 1 byte data length. Most converters default to a length of 32 bytes. The length just indicates the number of Data bytes and does not include the other fields. After the length byte you get a 2 byte address field for the data.

Intel specified 6 record types as shown below. Bin2Hex support all but the last 2 types which are used for (Intel) processors that can address more than 1Mbyte, e.g. a 386. In most cases a hex file consist of data records (00) followed by an EOF record (01).

00 Data Record
0l End of File Record
02 Extended Segment Address Record
03 Start Segment Address Record
04 Extended Linear Address Record
05 Start Linear Address Record

After the data you get a fairly simple checksum which consist of a two's compliment modulo 256 addition of all the bytes excluding the start character and the checksum itself.

Examples

Input binary file d.bin (displayed using cygwin's dump program)

Address   0 1  2 3  4 5  6 7  8 9  A B  C D  E F
--------  ---- ---- ---- ---- ---- ---- ---- ----
00000000  4350 5538 3820 426f 6f74 6c6f 6164 6572
00000010  2052 6561 6479 3e0a 0d00 8cc8 8ed8 33c0
00000020  8ec0 06bf 0001 57cb 5250 baf9 02ec 2402
00000030  74fb baf8 0258 ee5a c352 baf9 02ec 2401

BIN2HEX d.bin d.hex

:20000000435055383820426F6F746C6F616465722052656164793E0A0D008CC88ED833C046
:200020008EC006BF000157CB5250BAF902EC240274FBBAF80258EE5AC352BAF902EC240183
:00000001FF

Binary file is converted to hex starting at address 0x0000.

BIN2HEX d.bin d.hex -o 0100

:20010000435055383820426F6F746C6F616465722052656164793E0A0D008CC88ED833C045
:200120008EC006BF000157CB5250BAF902EC240274FBBAF80258EE5AC352BAF902EC240182
:00000001FF

The starting load address has changed to 0x0100.

BIN2HEX d.bin d.hex -o 0100 -s 0200

:020000020200FA
:20010000435055383820426F6F746C6F616465722052656164793E0A0D008CC88ED833C045
:200120008EC006BF000157CB5250BAF902EC240274FBBAF80258EE5AC352BAF902EC240182
:00000001FF

The -s command will result in an extra "02" record being generated (first line). The 02 record is an extended address record and is normally used to change the 8086/8088 segment register (CS). If the above file was loaded on an 8086 system then the starting address would be 0200:0100 (0x02100).

BIN2HEX d.bin d.hex -o 0100 -s 0200 -e 013A

:020000020200FA
:20010000435055383820426F6F746C6F616465722052656164793E0A0D008CC88ED833C045
:200120008EC006BF000157CB5250BAF902EC240274FBBAF80258EE5AC352BAF902EC240182
:040000030200013ABC

To execute a program issue the -e command which replaces the EOF record (:00000001FF) with a so called Start Segment Address Record (03). In the above example the start address is 0200:013A. The EOF record is not required since the boostrap loader will jump to the execute address after verifying the checksum.

Download Utility

Some Links