Simple utils written in C to generate C code or statically linkable object files from binary files.
bin2c <infile> <outfile> <array_name>
Creates a C source file named outfile
containing the entire byte stream of infile
named array_name
.
Given the file helloworld.txt
containing:
Hello, world!
The command bin2c helloworld.txt data.c data_bytes
produces the file data.c
with the following output:
/* This file was automatically generated from the file helloworld.txt
data_bytes contains the raw byte stream of the entire file. */
const unsigned long data_bytes_size = 14;
const unsigned char data_bytes[14] =
{
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
0x21, 0x0a
};
bin2o <infile> <outfile> <array name> <header name>
Uses bin2c to create an intermediate C file as described above, then immediately compiles it to an object file and creates a corresponding header file so it can be linked and referenced.
The command bin2o helloworld.txt data.o data_bytes data.h
produces both data.h
:
// data.h
#ifndef BIN2O_data_h_H_INCLUDED
#define BIN2O_data_h_H_INCLUDED
/* Generated by bin2o from file helloworld.txt */
extern const unsigned long data_bytes_size;
extern const unsigned char data_bytes[];
#endif
and data.o
, which is to be linked statically and contains the data, of course.
Compile bin2c using gcc -o bin2c bin2c.c
or similar. Just make sure the executable is actually named bin2c
.
bin2o is a bash script, you might have to do chmod +x bin2o
.
Then make sure both are on your PATH, eg by copying them to /usr/bin/
.