views:

121

answers:

6

Please suggest a small command-line utility (for Windows) to convert files from particular directory to a valid c file. Maybe it can be done just with batch commands? The resulting file should look like this:

static const unsigned char some_file[] = {
    /* some_file.html */
    0x2f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x73, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0,
    0x25, 0x21, 0x3a, 0x20, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x65
}

static const unsigned char some_other_file[] = {
    /* some_other_file.png*/
    0x2f, 0x34, 0x30, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
    0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x20, 0x20, 0x3c
}

P.S. Please don't suggest Perl and Python ports. They are too heavy for this task.

P.P.S. May be someone knows more customizable utility than bin2h, but less heavy and complex than awt? Which can parse several files and put them into one C. Also specifing custom variable names (using some kind of an index file) whould be great. So it can be added to the build process.

A: 

There's a windows port of awk that you could use.

stu
+5  A: 

Bin2h will do this.

Bin2h - Win32 binary to C header file converter

A Win32 command-line utility for converting a binary file into a C header file, representing the contents of that file as a block of data.

I don't believe the input file has to be a binary file.

luke
Note that 'bin2h' cannot be used for commercial purposes without seeking permission from the author - but it seems to do the job requested.
Jonathan Leffler
probably that's it.
cos
Accepted after 5 minutes! Confident of no better answer then?
Clifford
Hey - he was asked to work on his accept rate - what's he to do? (jk)
Michael Burr
I can change accepted answer at any time. (as far as I know)
cos
A: 

G'day,

While you're looking at awk I'd suggest having a look at the complete range of the cygwin utilities.Lots of useful stuff in there.

Rob Wells
+2  A: 

Use xxd -i file.

I use the one included with Vim. For example:

C:\Documents and Settings\user> xxd -i error.log | head -n2
unsigned char error_log[] = {
  0x0c, 0x0d, 0x0a, 0x3d, 0x3d, 0x32, 0x30, 0x30, 0x39, 0x2f, 0x35, 0x2f,

See also Is it possible to view a binary in ones and zeros?

Sinan Ünür
The version of xxd on MacOS X 10.5.8 ('xxd -v' says 'xxd V1.10 27oct98 by Juergen Weigert') does not seem to support C code generation.
Jonathan Leffler
@Jonathan Leffler: The version I compiled alongside `vim` on Windows gives me the same version string: `xxd V1.10 27oct98 by Juergen Weigert (Win32)` yet it does support C code generation. It must be a compile time option.
Sinan Ünür
@Sinan: Nope - It seems to be a PEBKAC; I can't read the man page in a hurry. The 'xxd -i' option works fine on MacOS X.
Jonathan Leffler
A: 

SRecord can do that, and more. Though it is hardly difficult to write your own in C.

Clifford
+1  A: 

If you want a utility that can be freely used (commercial or whatever) here's a GPL bin2c by Adrian Prantl:

It's a single 70 line or so C file - nothing to it to compile and run.

Michael Burr