简体   繁体   English

将任何类型的文件转换为带有c字符串的文件

[英]Convert files of any types to a file with c strings

Please suggest a small command-line utility (for Windows) to convert files from particular directory to a valid c file. 请建议一个小的命令行实用程序(用于Windows)将文件从特定目录转换为有效的c文件。 Maybe it can be done just with batch commands? 也许它可以用批处理命令完成? The resulting file should look like this: 生成的文件应如下所示:

static const unsigned char some_file[] = {
    /* some_file.html */
    0x2f, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x73, 0x2e, 0x73, 0x68, 0x74, 0x6d, 0x6c, 0,
    0x25, 0x21, 0x3a, 0x20, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x65
}

static const unsigned char some_other_file[] = {
    /* some_other_file.png*/
    0x2f, 0x34, 0x30, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
    0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x20, 0x20, 0x3c
}

PS Please don't suggest Perl and Python ports. PS请不要建议Perl和Python端口。 They are too heavy for this task. 它们对于这项任务来说太沉重了。

PPS May be someone knows more customizable utility than bin2h, but less heavy and complex than awt? PPS可能有人知道比bin2h更可定制的实用程序,但是比awt更重,更复杂? Which can parse several files and put them into one C. Also specifing custom variable names (using some kind of an index file) whould be great. 哪个可以解析几个文件并将它们放到一个C中。同时指定自定义变量名称(使用某种索引文件)也很棒。 So it can be added to the build process. 因此可以将其添加到构建过程中。

Use xxd -i file . 使用xxd -i file

I use the one included with Vim . 我使用Vim附带的那个。 For example: 例如:

C:\Documents and Settings\user> xxd -i error.log | head -n2
unsigned char error_log[] = {
  0x0c, 0x0d, 0x0a, 0x3d, 0x3d, 0x32, 0x30, 0x30, 0x39, 0x2f, 0x35, 0x2f,

See also Is it possible to view a binary in ones and zeros? 另请参见是否可以以1和0查看二进制文件?

Bin2h will do this. Bin2h会这样做。

Bin2h - Win32 binary to C header file converter Bin2h - Win32二进制到C头文件转换器

A Win32 command-line utility for converting a binary file into a C header file, representing the contents of that file as a block of data. 一个Win32命令行实用程序,用于将二进制文件转换为C头文件,将该文件的内容表示为数据块。

I don't believe the input file has to be a binary file. 我不相信输入文件必须是二进制文件。

If you want a utility that can be freely used (commercial or whatever) here's a GPL bin2c by Adrian Prantl: 如果你想要一个可以自由使用的工具(商业或其他),这里是Adrian Prantl的GPL bin2c:

/*
 * bin2c: A Program to convert binary data into C source code
 * Copyright 2004 by Adrian Prantl <adrian@f4z.org>
 *
 *   This program is free software; you can redistribute it and/or modify  
 *   it under the terms of the GNU General Public License as published by  
 *   the Free Software Foundation; either version 2 of the License, or     
 *   (at your option) any later version.
 *
 */

#include <stdio.h>
#include <stdlib.h>

char* self = 0;

void usage() {
    printf("Usage:\n%s input.bin output.h name\n\n", self);
}

void bail_out(const char* s1, const char* s2) {
    fprintf(stderr, "%s: FATAL ERROR:\n%s%s\n", self, s1, s2);
    exit(1);
}

int main(int argc, char** argv) {
    FILE *fi, *fo;
    int c, i;

    self = argv[0];

    if (argc != 4) {
        usage();
        return 0;
    }

    if ((fi = fopen(argv[1], "rb")) == 0)
        bail_out("Cannot open input file ", argv[1]);

    if ((fo = fopen(argv[2], "w")) == 0)
        bail_out("Cannot open output file ", argv[2]);

    if ((c = fgetc(fi)) != EOF) {
        fprintf(fo, "#ifndef %s_H\n", argv[3]);
        fprintf(fo, "#define %s_H\n\n", argv[3]);
        fprintf(fo, "const unsigned char %s[] = {\n", argv[3]);
        fprintf(fo, c < 16 ? "   0x%02x" : "    0x%02x", (unsigned char) c);
    }

    i = 1;
    while ((c = fgetc(fi)) != EOF) {
        if (i < 12)
            fprintf(fo, c < 16 ? ", 0x%02x" : ", 0x%02x", (unsigned char) c);
        else {
            fprintf(fo, c < 16 ? ",\n   0x%02x" : ",\n   0x%02x", (unsigned char) c);
            i = 0;
        }
        i++;
    }
    fprintf(fo, "\n};\n\n");
    fprintf(fo, "#endif\n");

    printf("converted %s\n", argv[1]);

    return 0;
}

It's a single 70 line or so C file - nothing to it to compile and run. 这是一个70行左右的C文件 - 编译和运行没什么。

SRecord can do that, and more. SRecord可以做到这一点,等等。 Though it is hardly difficult to write your own in C. 虽然用C语写你自己很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM