[go: up one dir, main page]

0% found this document useful (0 votes)
47 views6 pages

GENERAL - Creating C Files From Binary or Hex Data

This document provides guidance on converting binary or hex data into C source files using the srec_cat.exe utility. It explains how to create a command file to automate the conversion process and includes examples of the generated C code. Additionally, it outlines various parameters for customizing the conversion process and links to further resources for more detailed information.

Uploaded by

vira.predeus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

GENERAL - Creating C Files From Binary or Hex Data

This document provides guidance on converting binary or hex data into C source files using the srec_cat.exe utility. It explains how to create a command file to automate the conversion process and includes examples of the generated C code. Additionally, it outlines various parameters for customizing the conversion process and links to further resources for more detailed information.

Uploaded by

vira.predeus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

17.02.

25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

(/)

⌂ (/) Products (/product/) Download (/download/) Events (/events/) Search Keil... + Go

Support (/support/) Videos (http://www2.keil.com/video)

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 (/company/privacy) to learn
more about our collection, use and
transfers
of your data.

Accept and hide this message


Important information

This site uses cookies to store


information on your computer. By
continuing to use our site, you
consent to our cookies
(/company/cookiepolicy).

Don't show this message again

Change Settings
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 1/6
17.02.25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

Technical Support Home (/) / Technical Support


Overview (/support/)
Search (/home/searchhelp)
Contact (/support/contact.asp)
GENERAL: Creating C Files from Binary or Hex Data
Assistance Request (/support/request.asp)
Feedback (/support/feedback.asp) Information in this article applies to:

Support Resources Keil MDK All Versions


Support Knowledgebase (/support/knowledgebase.asp)
C51 All Versions
Article Index (/support/topics.asp)
C251 All Versions
Top 10 Articles (/support/topten.asp)
C166 All Versions
Product Manuals (/support/man/)
Application Notes (/appnotes/)
Downloads (/download/) QUESTION
Product Updates (/update/)
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
Discussion Forum (/forum/)
this?
Books (/books/)
ANSWER
Product Information
Software & Hardware Products (/product/) 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 (https://sourceforge.net/projects/srecord/files/srecord-
Arm Development Tools (/arm/)
win32). It is part of the SRecord project hosted on sourceforge.net. With this utility, you can load one or multiple
C166Privacy
Development Tools (/c166/)
Policy Update
HEX or BINARY files and store the data into an C-source file as an initialized C const array.
C51
Arm’sDevelopment Tools
Privacy Policy has (/c51/)
been
updated.
C251 Development Tools use
By continuing to our
(/c251/)
site, you consent to Arm’s Privacy Since a few parameters are necessary to specify all filenames and the name of the C-array, it is easier to invoke
Policy.
Debug Please
Adaptersreview our Privacy
(/ulink/) srec_cat.exe with a command file. With the following command file MyBin2Const.cmd, srec_cat.exe reads the
Policy (/company/privacy) to learn
Evaluation Boards (/boards2/)
more about our collection, use and file MyBinFile.bin and creates the files MyBinFile.c and MyBinFile.h which contain a const array with the name
transfers
Product Brochures (/product/brochures.asp) MyBinaryImage[].
of your data.
Newsletters (/product/newsletters.asp)
MyBinFile.bin -Binary # input file has binary format
Important information -o MyBinFile.c # output file name
-C-Array MyBinaryImage -INClude # generate C-array with the name MyBinaryImage
This site uses cookies to store
information on your computer. By # and an include file
continuing to use our site, you
consent to our cookies When a hex file should be converted, exchange 'MyBinFile.bin -Binary' with 'MyHexFile.hex -Intel'.
(/company/cookiepolicy).

Example for a created MyBinFile.c file:

Change Settings
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 2/6
17.02.25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

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
Privacy Policy Update
(http://www.keil.com/support/man/docs/uv4/uv4_ut_keysequence.htm) for the invocation. When using a
Arm’s Privacy Policy has been
updated. By continuing to use our
command file for srec_cat.exe, be sure to double the '@' character, or µVision might interpret it as a key-
site, you consent to Arm’s Privacy sequence.
Policy. Please review our Privacy
Policy (/company/privacy) to learn
more about our collection, use and
transfers
of your data.

Important information

This site uses cookies to store


information on your computer. By
continuing to use our site, you
consent to our cookies
(/company/cookiepolicy).

Change Settings
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 3/6
17.02.25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

For a full description of all srec_cat.exe parameters, refer to the SRecord Reference Manual
(http://srecord.sourceforge.net/srecord-1.64.pdf). 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.
Privacy Policy Update -fill FillValue StartAddress EndAddress
Arm’s Privacy Policy has been Fills unused areas with the specified constant value. A fill value of 0xFF is often used with this option
updated. By continuing to use our because it corresponds to erased Flash.
site, you consent to Arm’s Privacy
Policy. Please review our Privacy -crop StartAddress EndAddress
Policy (/company/privacy) to learn
more about our collection, use and Only loads the specified address area from the previous input file. This option can be combined with -offset.
transfers
of your data. -offset Offset
Adds an address offset to the previous input file. Positive or negative values are allowed. This option can be
combined with -crop.
Important information
-Intel
This site uses cookies to store
information on your computer. By Can be used after an input or output filename to specify that an input file should be interpreted as an Intel
continuing to use our site, you
consent to our cookies HEX file or an output file should be generated as an Intel HEX file.
(/company/cookiepolicy).
-Binary
Can be used after an input or output filename to specify that an input file should be interpreted as a binary
Change Settings file or an output file should be generated as a binary file.
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 4/6
17.02.25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

-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
Refer to SRecord Reference Manual (http://srecord.sourceforge.net/srecord-1.64.pdf)
Refer to Start External Tools
(http://www.keil.com/support/man/docs/uv4/uv4_ca_start_external_tools.htm) in the µVision User's
Guide.
Refer to Key Sequence for Tool Parameters
(http://www.keil.com/support/man/docs/uv4/uv4_ut_keysequence.htm) in the µVision User's Guide.

SEE ALSO
ARM: Include a Binary Image into an Application (http://www.keil.com/support/docs/3770.htm)
GENERAL: Converting HEX, Binary, etc. File Formats (http://www.keil.com/support/docs/4038.htm)
GENERAL: Intel HEX File Format (http://www.keil.com/support/docs/1584.htm)
ARMLINK: Generating Binary Output During a Build (http://www.keil.com/support/docs/3213.htm)

Privacy Policy Update

Arm’s Privacy Policy has been Last Reviewed: Thursday, February 25, 2021
updated. By continuing to use our
site, you consent to Arm’s Privacy
Policy. Please review our Privacy
Policy (/company/privacy) to learn
more about our collection, use and Did this article provide the
transfers
of your data.
answer you needed?

Important information Yes


This site uses cookies to store
No
information on your computer. By Not Sure
continuing to use our site, you
consent to our cookies
(/company/cookiepolicy).
Submit

Change Settings
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 5/6
17.02.25, 08:23 GENERAL: Creating C Files from Binary or Hex Data

Products (/product/) Downloads (/download/) Support (/support/) Contact


Development Tools Hardware & Collateral MDK-Arm (/demo/eval/arm.htm) Knowledgebase (/support/knowledgebase.asp) Distributors (/distis/)
Arm (/Arm/) ULINK Debug Adaptors (/ulink/) C51 (/demo/eval/c51.htm) Discussion Forum (/forum/) Request a Quote (/product/prices.asp)
C166 (/c166/) Evaluation Boards (/boards2/) C166 (/demo/eval/c166.htm) Product Manuals (/support/man/) Sales Contacts (/company/contact/)
C51 (/c51/) Product Brochures (/product/brochures.asp) C251 (/demo/eval/c251.htm) Application Notes (/appnotes/)
C251 (/c251/) Device Database (/dd2/) File downloads (/download/file/)
µVision IDE and Debugger (/uvision/) Distributors (/distis/)

Cookie Settings (/company/cookiesettings) | Terms of Use (/company/terms) | Privacy (/company/privacy) | Accessibility


(/company/accessibility) | Trademarks (https://www.arm.com/company/policies/trademarks) | Contact Us (/company/contact/) | Feedback
(/support/feedback.asp)
Copyright (/company/terms) © 2005-2019 Arm Limited (/company) (or its affiliates). All rights reserved.

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 (/company/privacy) to learn
more about our collection, use and
transfers
of your data.

Important information

This site uses cookies to store


information on your computer. By
continuing to use our site, you
consent to our cookies
(/company/cookiepolicy).

Change Settings
(/company/cookiesettings/)

https://www.keil.com/support/docs/2218.htm 6/6

You might also like