Convert a File to a C Data Structure
June 13th, 2008 by Mitch Frazier in
Yesterday's post about embedding a file in an executable has gotten a couple of replies about programs that will convert the file to a C data structure. This is certainly an option, here's a script that does that without the need to go searching for programs, it uses standard Linux tools:
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: $0 FILENAME"
exit 1
fi
file=$1
if [[ ! -f "$file" ]]; then
echo "File not found: $file"
exit 1
fi
cname=$file
cname=${cname//-/_}
cname=${cname//./_}
echo "static unsigned char $cname[] = {"
hexdump -v -e '" " 16/1 " 0x%02x, " "\n"' $file | \
sed -e '$s/0x ,//g'
echo "};"
PS This script also appears in a reply to the original post.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Nov-04-09
- Oct-29-09
- Oct-26-09
Recently Popular
From the Magazine
December 2009, #188
If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.
Delicious
Digg
StumbleUpon
Reddit
Facebook








Another option is using xxd
On June 14th, 2008 Anonymous (not verified) says:
Another option is using
xxd -i. xxd is part of the vim package.sdsd
On June 13th, 2008 Anonymous (not verified) says:
very interesting
Post new comment