Script / tool to convert a file to an array of C / C ++ source code

I need a script / tool that reads a binary file and outputs an array of C / C ++ source code (which represents the contents of the file). Whether there is a?




(This question was deleted earlier. I returned this question because it is valuable. I looked for exactly this on Google and did not find anything. Of course, it is simple, but I would save some minutes if I found such a simple script, so it valuable.

These questions also had many votes without explanation. Please comment before you vote why you think it does not matter or has a bad value.

This question also caused a lot of confusion as to what I am asking. If something is unclear, please ask. I do not know how to make this more clear. See Answers for examples.

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

+71
c binary
Jan 03 2018-12-12T00:
source share
6 answers

In Debian and other Linux distributions, the xxd tool is installed by default (along with vim ), 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; 
+121
Jan 03 '12 at 2:17
source share

One simple tool can be found here :

 #include <stdio.h> #include <assert.h> int main(int argc, char** argv) { assert(argc == 2); char* fn = argv[1]; FILE* f = fopen(fn, "rb"); printf("char a[] = {\n"); unsigned long n = 0; while(!feof(f)) { unsigned char c; if(fread(&c, 1, 1, f) == 0) break; printf("0x%.2X,", (int)c); ++n; if(n % 10 == 0) printf("\n"); } fclose(f); printf("};\n"); } 
+6
Jan 03 2018-12-12T00:
source share

The accepted answer using the xxd tool is good if you are working on a * nix-like system. Here is the "one line" for any system that has a python executable file:

 python -c "import sys;a=sys.argv;open(a[2],'wb').write(('const unsigned char '+a[3]+'[] = {'+','.join([hex(b) for b in open(a[1],'rb').read()])+'};').encode('utf-8'))" <binary file> <header file> <array name> 

<binary file> is the name of the file you want to turn into a C header, <header file> is the name of the header file, and <array name> is the name the array should have.

The above single-line Python command does roughly the same thing as the following (much more readable) Python program:

 import sys with open(sys.argv[2],'wb') as result_file: result_file.write(b'const char %s[] = {' % sys.argv[3].encode('utf-8')) for b in open(sys.argv[1], 'rb').read(): result_file.write(b'0x%02X,' % b) result_file.write(b'};') 
+1
Jun 19 '19 at 13:37
source share

This tool is compiled on the developer's command line in C. It outputs a terminal output that displays the contents in the file "array_name.c" that is created. Please note that some terminals may display the character "\ b".

  #include <stdio.h> #include <assert.h> int main(int argc, char** argv) { assert(argc == 2); char* fn = argv[1]; // Open file passed by reference FILE* f = fopen(fn, "rb"); // Opens a new file in the programs location FILE* fw = fopen("array_name.c","w"); // Next two lines write the strings to the console and .c file printf("char array_name[] = {\n"); fprintf(fw,"char hex_array[] = {\n"); // Declare long integer for number of columns in the array being made unsigned long n = 0; // Loop until end of file while((!feof(f))){ // Declare character that stores the bytes from hex file unsigned char c; // Ignore failed elements read if(fread(&c, 1, 1, f) == 0) break; // Prints to console and file, "0x%.2X" ensures format for all // read bytes is like "0x00" printf("0x%.2X,", (int)c); fprintf(fw,"0x%.2X,", (int)c); // Increment counter, if 20 columns have been made, begin new line ++n; if(n % 20 == 0){ printf("\n"); fprintf(fw,"\n"); } } // fseek places cursor to overwrite extra "," made from previous loop // this is for the new .c file. Since "\b" is technically a character // to remove the extra "," requires overwriting it. fseek(fw, -1, SEEK_CUR); // "\b" moves cursor back one in the terminal printf("\b};\n"); fprintf(fw,"};\n"); fclose(f); fclose(fw); } 
0
Nov 29 '17 at 21:09 on
source share

This is the binary for the Python source code of the C array generator, identical to the program in Albert's answer .

 import sys from functools import partial if len(sys.argv) < 2: sys.exit('Usage: %s file' % sys.argv[0]) print("char a[] = {") n = 0 with open(sys.argv[1], "rb") as in_file: for c in iter(partial(in_file.read, 1), b''): print("0x%02X," % ord(c), end='') n += 1 if n % 16 == 0: print("") print("};") 
0
Sep 28 '18 at 11:47
source share

The question is old, but let me suggest a simple tool that can be used as an alternative ...

You can use a graphical tool called Fluid. It is actually used to develop an interface for the FLTK toolkit, but can also generate an unsigned array for C ++ from a binary file. Download it from Muquit .

Fluid screenshot

0
Dec 31 '19 at 13:58
source share



All Articles