GENERAL: Converting HEX, Binary, etc. File Formats
Information in this article applies to:
- C166 any version
- C251 any version
- C51 any version
- MDK any version
QUESTION
What Options are there for manipulating converting HEX and Binary
file formats? There are older tools that did this, but they do not
run on 64-bit operating systems.
ANSWER
The command line utility srec_cat.exe is an
excellent and more versatile replacement for HEX2BIN, BIN2HEX,
BIN2MOT and MOT2BIN.
This tool is part of the SRecord package version
1.64 hosted on sourceforge.net. SRECORD contains
utilities to read and manipulate Intel HEX files in many ways. From
the summary page of that site:
"The SRecord package is a collection of powerful tools for
manipulating EPROM load files. It reads and writes numerous EPROM
file formats, and can perform many different manipulations."
Other useful features of srec_cat.exe:
-
Reads Hex files made by Keil OH51, OHX51, OH251 or OH166 or
fromelf.exe
- Always stores Intel HEX records in ascending order
- Can be invoked from within the Keil IDE
- Runs on Windows OS
Sections in this article:
USE CASES
Please see the following knowledgebase articles how HEX and BINARY
files can be converted, merged and split using the
srec_cat.exe utility together with Keil
microcontroller development tools.
General HEX File Manipulation
HEX File Manipulation for 8051 Devices
HEX File Manipulation for ARM Devices
General BINARY File Manipulation
Other Applications
TUTORIAL
Setup the tools to execute srec_cat.exe after
each project build/rebuild.
- Open µVision
-
Select the drop down menu Project ->
Options for Target** and click on the User
tab
-
Below After Build/Rebuild Check the box for
Run #1
-
For user command, call srec_cat.exe with
command options. The key
sequence
#H
can be used for the input hex file
name.
- For Example:
srec_cat.exe #H -Intel -o SortedHexFile.hex -Intel
Note: If many parameters are needed, it is easier
to use a command file. When using a command file for srec_cat.exe in
µVision, be sure to double the '@' character, or µVision might
interpret it as a key
sequence.

SYNTAX
For a full description of all srec_cat.exe parameters, refer to
the SRecord Reference Manual. Here are some
options which are useful with Keil tools:
-
-DisableSequenceWarnings
Suppress the warning message that the HEX records of the input file
are not sorted in ascending address order. OH51, OHX51, OH251 or
OH166 cannot generate HEX files with ascending address order.
-
-address-length= 2 or 3 or 4
The number of address bytes in the Intel HEX output file (default
is 4: generate extended address records (type 04)
for an address range of up to 4GB). In the command string,
address-length
must be before -o
(specify
an output file).
For non-code-banked 8051 applications: avoid generating
extended address records. Set -address-length=2
to
limit the maximum address space to 64K. Code banked application can
be larger than than 64K.
-
-OutputBlockSize= ByteCount
The length of each HEX record (default is 32:
lines contain up to 32 bytes of data).
For C51, C251, C166 applications: set
-OutputBlockSize=16
(compatible with OH51, OHX51,
OH251 or OH166).
-
-fill FillValue StartAddress EndAddress
Fills unused areas with the specified constant value.
Suggestion: use the value 0xFF - this
corresponds to value of erased flash.
-
-crop StartAddress EndAddress
Only loads the specified address area from the previous input file.
Combine this option with -offset
.
-
-offset Offset
Add an address offset to the previous input file. Positive or
negative values are allowed. Combine this option with
-crop
.
-
-Intel
Treat the preceding file name as Intel Hex, e.g., [input filename]
-Intel
= interpret the input file as an Intel HEX, or
[output filename] -Intel
= generate an output file in
Intel HEX
-
-Binary
treat the preceding file name is a binary, e.g., [input filename]
-Binary
= interpret the input file as a binary, or
[output filename] -Binary
= generate an output file in
binary
-
@CommandFile
A command file can contain some or all invocation parameters for
srec_cat.exe.
Tip: Start a line with '#' to comment everything to the
end of that line.
-
-Motorola
Can be used after an input or output filename to specify that an
input file should be interpreted as a Motorola S-Record file or an
output file should be generated as a Motorola S-Record file.
-
-C-Array filename [-INClude]
Can be used after an output filename to specify that it should be
generated as a C-source file with an optional include file.
SEE ALSO
SEE ALSO FOR 8051
Last Reviewed: Thursday, February 25, 2021