hexembed is a very small utility to help embed files in C or C++ programs in an easy, cross-platform way.
> gcc hexembed.c -o hexembed
> hexembed some_file.jpg > some_file.c
> cat some_file.c
/* Embedded file: some_file.jpg */
const int fsize = 1873;
const unsigned char *file = {
0x2f,0x2a,0x0a,0x20,0x2a,0x20,0x68,0x65,0x78,0x65,0x6d,0x62,0x65,0x64,0x20,0x2d,
0x20,0x61,0x20,0x73,0x69,0x6d,0x70,0x6c,0x65,0x20,0x75,0x74,0x69,0x6c,0x69,0x74,
0x79,0x20,0x74,0x6f,0x20,0x68,0x65,0x6c,0x70,0x20,0x65,0x6d,0x62,0x65,0x64,0x20,
0x66,0x69,0x6c,0x65,0x73,0x20,0x69,0x6e,0x20,0x43,0x20,0x70,0x72,0x6f,0x67,0x72,
...
};
Now you can simply #include "some_file.c"
file in your program, and you have access to that file's data.
You can find more info and alternative methods here: https://codeplea.com/embedding-files-in-c-programs
Modified by hiornso 2021-12-09 to add
- arguments to specify infile, outfile and variable name
- make it stream rather than cache in buffer
- have an
alignment
global var to allow easy setting of alignment of data