Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert files of any types to a file with c strings

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.

like image 877
cos Avatar asked Nov 10 '09 19:11

cos


1 Answers

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?

like image 103
Sinan Ünür Avatar answered Oct 18 '22 20:10

Sinan Ünür