A simple program that can embed binary or text files into source code of a specific language
You can find pre-built releases for linux, macOS and Windows
Otherwise, you can install embin from source using cargo, with the following command:
cargo install embin⭐ Don't forget to put a star if you like the project!
Usage: embin [OPTIONS] <INPUT>...
Arguments:
<INPUT>... Input file(s) to embed, which can be binary or text files
Options:
-o, --output <OUTPUT> Write generated source code in output file instead of stdout
--lang <LANG> Language of the generated source code [default: c]
--format <FORMAT> Format of the generated source code [default: hex]
--indent <INDENT> Indentation type of the generated source code [default: space]
--padding <PADDING> Padding value of the generated source code [default: 4]
--quantity <QUANTITY> Number of byte elements per line [default: 16]
--mutable Make generated variables mutable
-h, --help Print help
-V, --version Print version
embin --lang=c data.png > output.hResult:
const unsigned char data_png[] = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
};
const unsigned int data_png_len = 42;embin --lang=cpp data.png > output.hppResult:
#include <array>
constexpr std::array<unsigned char, 42> data_png = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
};embin --lang=python data.png > output.pyResult:
DATA_PNG = bytes([
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
])
⚠️ On Windows, use--outputinstead of>when generating your embedded assets to avoid the following error when running python code:
source code string cannot contain null bytes
embin --lang=cpp assets/icon.png assets/banner.png > assets.hppResult:
#include <array>
constexpr std::array<unsigned char, 42> icon_png = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
};
constexpr std::array<unsigned char, 86> banner_png = {
0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x6c, 0x69, 0x6e,
0x65, 0x2e, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61,
0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a,
0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e,
0x65, 0x77, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a, 0x54, 0x68, 0x69,
0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20,
0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20,
0x73, 0x20
};Char format:
embin --format=char data.txtconst unsigned char data_txt[] =
"This is a test file.\n\n"
"This is a new line.\n";
const unsigned int data_txt_len = 42;Octal format:
embin --format=octal data.txtconst unsigned char data_txt[] =
"\124\150\151\163\040\151\163\040\141\040\164\145\163\164\040\146"
"\151\154\145\056\012\012\124\150\151\163\040\151\163\040\141\040"
"\156\145\167\040\154\151\156\145\056\012";
const unsigned int data_txt_len = 42;Indent with tabs instead of spaces:
embin --indent=tab data.pngembin --padding 0 data.pngconst unsigned char data_png[] = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
};
const unsigned int data_png_len = 42;embin --quantity 8 data.pngconst unsigned char data_png[] = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x66,
0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
0x6e, 0x65, 0x77, 0x20, 0x6c, 0x69, 0x6e, 0x65,
0x2e, 0x0a
};
const unsigned int data_png_len = 42;embin --mutable data.pngunsigned char data_png[] = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x0a, 0x0a, 0x54, 0x68,
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6e, 0x65, 0x77, 0x20,
0x6c, 0x69, 0x6e, 0x65, 0x2e, 0x0a
};
unsigned int data_png_len = 42;This project is released under MIT license.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.