6

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.

cos
  • 940
  • 1
  • 10
  • 25

4 Answers4

15

Use xxd -i file.

I use the one included with 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?

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
5

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.

luke
  • 36,103
  • 8
  • 58
  • 81
3

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:\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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • What's the purpose of `c < 16` in the `while` loop? The second and third part of the ternary operator are identical, so it's just a waste of instructions, not? – Rob W Jan 17 '13 at 18:09
  • @RobW: I'd guess that at some point there was a difference that went away; perhaps something like `c < 16 ? ", 0x0%x" : ", 0x%x"`. I'd agree that getting rid of those expressions would make sense. – Michael Burr Jan 17 '13 at 18:46
0

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

Clifford
  • 88,407
  • 13
  • 85
  • 165