Keil Logo

GENERAL: CREATING C FILES FROM BINARY OR HEX DATA


Information in this article applies to:

  • MDK-ARM All Versions
  • C51 All Versions
  • C251 All Versions
  • C166 All Versions

QUESTION

I have some binary data that I want to convert to a C array to use in my application. What's the best way to do this?

ANSWER

You may want to consider using the free srec_cat.exe utility which is available as Windows EXE file from https://sourceforge.net/projects/srecord/files/srecord-win32. It is part of the SRecord project hosted on sourceforge.net. With this utility, you can load one or multiple HEX or BINARY files and store the data into an C-source file as an initialized C const array.

Since a few parameters are necessary to specify all filenames and the name of the C-array, it is easier to invoke srec_cat.exe with a command file. With the following command file MyBin2Const.cmd, srec_cat.exe reads the file MyBinFile.bin and creates the files MyBinFile.c and MyBinFile.h which contain a const array with the name MyBinaryImage[].

MyBinFile.bin -Binary              # input file has binary format
-o MyBinFile.c                     # output file name
-C-Array MyBinaryImage -INClude    # generate C-array with the name MyBinaryImage
                                   # and an include file

When a hex file should be converted, exchange 'MyBinFile.bin -Binary' with 'MyHexFile.hex -Intel'.

Example for a created MyBinFile.c file:

const unsigned char MyBinaryImage[] =
{
0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5, 0xF4,
0xF3, 0xF2, 0xF1, 0xF0, 0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8,
...
...
0x03, 0x02, 0x01, 0x00,
};
const unsigned long MyBinaryImage_termination = 0x00000000;
const unsigned long MyBinaryImage_start       = 0x00000000;
const unsigned long MyBinaryImage_finish      = 0x00000400;
const unsigned long MyBinaryImage_length      = 0x00000400;

#define MYBINARYIMAGE_TERMINATION 0x00000000
#define MYBINARYIMAGE_START       0x00000000
#define MYBINARYIMAGE_FINISH      0x00000400
#define MYBINARYIMAGE_LENGTH      0x00000400

The generated C-source file MyBinFile.c can be added to the µVision project. The header file MyBinFile.h contains the declarations for the two variables MyBinaryImage[] and MyBinaryImage_length and can be included in other C-source files which need a reference to the binary data.

You can invoke srec_cat.exe from a Windows command prompt or Make tool:

srec_cat.exe @MyBin2Const.cmd

You can also invoke this tool after each project build/rebuild by specifying its invocation in the µVision dialog Options for Target - User - After Build/Rebuild. You may use µVision key sequences for the invocation. When using a command file for srec_cat.exe, be sure to double the '@' character, or µVision might interpret it as a key-sequence.

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:

  • -Disable_Sequence_Warnings
    This option suppresses a warning if records of the input Intel HEX file are not sorted in ascending address order. HEX file generated by OH51 or OHX51 are not sorted in ascending address order.
  • -fill FillValue StartAddress EndAddress
    Fills unused areas with the specified constant value. A fill value of 0xFF is often used with this option because it corresponds to erased Flash.
  • -crop StartAddress EndAddress
    Only loads the specified address area from the previous input file. This option can be combined with -offset.
  • -offset Offset
    Adds an address offset to the previous input file. Positive or negative values are allowed. This option can be combined with -crop.
  • -Intel
    Can be used after an input or output filename to specify that an input file should be interpreted as an Intel HEX file or an output file should be generated as an Intel HEX file.
  • -Binary
    Can be used after an input or output filename to specify that an input file should be interpreted as a binary file or an output file should be generated as a binary 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.
  • @CommandFile
    A command file can contain some or all invocation parameters of srec_cat.exe. You can even use comments starting with '#' extending to the end of the line.


MORE INFORMATION

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Tuesday, July 17, 2018


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.