Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

binary - 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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,
  0x0a
};
unsigned int temp_len = 13;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...