English 中文(简体)
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. 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
}

P.S. Please don t suggest Perl and Python ports. They are too heavy for this task.

P.P.S. May be someone knows more customizable utility than bin2h, but less heavy and complex than 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. So it can be added to the build process.

最佳回答

Bin2h will do this.

Bin2h - Win32 binary to C header file converter

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.

I don t believe the input file has to be a binary file.

问题回答

Use xxd -i file.

I use the one included with Vim. For example:

C:Documents and Settingsuser> 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?

If you want a utility that can be freely used (commercial or whatever) here s a GPL bin2c by Adrian Prantl:

/*
 * 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:
%s input.bin output.h name

", self);
}

void bail_out(const char* s1, const char* s2) {
    fprintf(stderr, "%s: FATAL ERROR:
%s%s
", 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
", argv[3]);
        fprintf(fo, "#define %s_H

", argv[3]);
        fprintf(fo, "const unsigned char %s[] = {
", 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 ? ",
   0x%02x" : ",
   0x%02x", (unsigned char) c);
            i = 0;
        }
        i++;
    }
    fprintf(fo, "
};

");
    fprintf(fo, "#endif
");

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

    return 0;
}

It s a single 70 line or so C file - nothing to it to compile and run.

SRecord can do that, and more. Though it is hardly difficult to write your own in C.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签