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
- Jul-01-09
- Jun-29-09
Recently Popular
From the Magazine
July 2009, #183
News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.
To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .
All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.
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