用于将文件转换为C / C ++源代码数组的脚本/工具

时间:2012-01-03 02:06:15

标签: c binary

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


(这个问题早些时候被删除。我把这个问题放回去了,因为它很有价值。我在谷歌上搜索的确没有找到任何东西。当然,编写自己的代码是微不足道的,但我本可以保存一些如果我能找到这样一个简单的剧本,那么它就是分钟。因此它很有价值。

这个问题也有很多下注而没有太多解释。请在你投票之前发表评论,为什么你认为这没有价值或价值不好。

这个问题也引起了很多关于我所问的问题的困惑。如果不清楚,请询问。我真的不知道如何更清楚。请参阅答案以获取示例。

另外(在这里提出问题之后),我已经有了几个答案。我只是想把它们(再次)放在/链接在一起,因为我认为对于其他搜索它的人来说它可能是有用的。)

7 个答案:

答案 0 :(得分:113)

在Debian和其他Linux发行版上默认安装vim xxd工具,-i选项,可以做你想做的事情:

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;

答案 1 :(得分:6)

可以找到一个简单的工具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");
}

答案 2 :(得分:1)

如果您使用的是类似* nix的系统,那么使用xxd工具接受的答案会很好。这是在路径上具有python可执行文件的任何系统的“单一代码”:

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>

<二进制文件>是要转换为C标头的文件的名称,<标头文件>是标头文件的名称,而<数组名>是希望数组具有的名称。 / p>

上面的单行Python命令与以下(可读性更高)Python程序大致相同:

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'};')

答案 3 :(得分:0)

此工具在C中的开发人员命令提示符中编译。它生成输出到终端,显示&#34; array_name.c&#34;中的内容。创建的文件。请注意,某些终端可能会显示&#34; \ b&#34;字符。

    #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);
}

答案 4 :(得分:0)

这是C数组生成器python源代码的二进制文件,它与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("};")

答案 5 :(得分:0)

这个问题很老,但让我建议一个简单的工具,可以将其用作替代...

您可以使用基于GUI的工具Fluid。它实际上用于设计FLTK工具包的接口,但也可以从二进制文件生成C ++的无符号字符数组。从muquit下载。

Fluid screenshot

答案 6 :(得分:0)

我检查了所有可用选项,并决定制作自己的小程序进行转换:

https://github.com/TheLivingOne/bin2array/blob/master/bin2array.c

它的运行速度比bin2c甚至xxd快得多,这对于较大的文件非常重要,尤其是如果要将转换嵌入到构建系统中时。例如。在我的计算机上获取50 Mb的文件:

bin2c.py> 20秒

简单的Python脚本-大约10秒

xxd-大约3秒

bin2array-大约0.4秒

此外,如果您想在其中放置32位或64位值,它还会产生更紧凑的输出并为数组添加对齐方式。