這個工具在開發者的命令提示符中編譯C.它產生的輸出到顯示創建的「array_name.c」文件中的內容的終端。請注意,某些終端可能會顯示「\ 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);
}
也許人們明白你想要某種反編譯器或類似的東西。你可以將其改寫爲「讀取二進制文件並輸出初始化爲文件內容的數組的C/C++聲明」或類似的內容。 – 2012-01-03 02:10:39