Albert Albert - 3 months ago 39
C Question

script/tool to convert file to C/C++ source code array

I need a script/tool which reads a binary file and outputs a C/C++ source code array (which represents the file content). Are there any?

(This question was deleted earlier. I put this question back in because it is valuable. I was searching for exactly this on Google and didn't found anything. Of course it is trivial to code myself but I would have saved some minutes if I would have found such a simple script. Thus it is valuable.

This questions also had a lot of down-votes without much explanation. Please comment before you down-vote why you think this is of no value or of bad value.

This question also caused a lot of confusion about what I am asking about. If something is unclear, please ask. I don't really know how to make it more clear. See the answers for examples.

Also (after putting the question here), I already have several answers. I just want to put/link them here (again) because I think it might be useful for someone else searching for this.)

Answer Source

On Debian and other Linux distros is installed by default (along with vim) the xxd tool, which, given the -i option, can do what you want:

matteo@teodeb:~/Desktop$ echo Hello World\! > temp
matteo@teodeb:~/Desktop$ xxd -i temp 
unsigned char temp[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21,
unsigned int temp_len = 13;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download