2012-01-03 120 views
49

我需要一个读取二进制文件并输出C/C++源代码数组(代表文件内容)的脚本/工具。有没有?脚本/工具将文件转换为C/C++源代码数组


(此问题被删除了前面。我把这个问题早在,因为它是有价值的。我正在寻找的正是这种在谷歌,并没有发现任何东西。当然,这是微不足道的自己,但我的代码如果我能找到这样一个简单的脚本,会节省一些时间,因此它是有价值的

这个问题也有很多没有太多解释的回应。请在评论之前发表评论为什么你认为这是没有价值或不好的价值

这个问题也引起了很多关于我在问什么的混淆如果有东西目前还不清楚,请询问。我真的不知道如何更清楚。查看示例的答案。

另外(在提出问题后),我已经有了几个答案。我只想把/他们在这里(再次)链接,因为我认为这可能是为别人寻找这很有用)

+2

也许人们明白你想要某种反编译器或类似的东西。你可以将其改写为“读取二进制文件并输出初始化为文件内容的数组的C/C++声明”或类似的内容。 – 2012-01-03 02:10:39

回答

82

在Debian和其他Linux发行版默认安装(连同vim)的xxd工具,其中,给予-i选项,可以做你想做什么:

[email protected]:~/Desktop$ echo Hello World\! > temp 
[email protected]:~/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; 
+4

好啊!它甚至可以在MacOSX中使用。 – Albert 2012-01-03 05:02:45

+0

正是我在找:)谢谢! – 2013-04-16 12:14:09

+2

将xxd集成到Windows上的Visual Studio 2013解决方案时没有问题。我用[此源](http://www.opensource.apple.com/source/vim/vim-43/vim/src/xxd/xxd.c?txt) – Spike0xff 2015-12-03 15:23:10

5

一个简单的工具,可以发现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"); 
} 
+0

在“}”之前的字符数组末尾会有多余的“,” – rkosegi 2013-01-04 14:30:32

+0

这不是问题,它使用C++进行编译。 – sashoalm 2013-08-28 08:08:07

0

这个工具在开发者的命令提示符中编译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); 
}