Loading
Home ›
Convert a File to a C Data Structure
Jun 13, 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.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
3 hours 5 min ago - Sure the best distro is
4 hours 26 min ago - BeOS was the best
7 hours 9 min ago - I use Wireshark on a daily
11 hours 40 min ago - buena información
16 hours 46 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
17 hours 47 min ago - Gnome3 is such a POS. No one
1 day 3 hours ago - Gnome 3 is the biggest POS
1 day 3 hours ago - I didn't knew this thing by
1 day 9 hours ago - Author's reply
1 day 12 hours ago





Comments
Another option is using xxd
Another option is using
xxd -i. xxd is part of the vim package.sdsd
very interesting