#embed #assets #deployment #language #xxd

app embin

将二进制或文本文件嵌入特定语言的源代码中

6 个稳定版本

1.1.4 2023年12月1日
1.1.3 2023年10月26日
1.1.2 2023年4月3日
1.1.0 2023年3月31日

开发工具类别中排名第 221

每月下载量 28

MIT 许可证 MIT

20KB
319 行代码(不包括注释)

embin

一个可以将二进制或文本文件嵌入特定语言源代码的简单程序

crates ci

安装

您可以在 linuxmacOSWindows 上找到预构建的 发布版本

否则,您可以使用以下命令从源代码安装 embin,使用 cargo

cargo install embin

⭐ 不要忘记如果您喜欢这个项目加一颗星表示支持!

用法

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

示例

将文件嵌入 C 语言

embin --lang=c data.png > output.h

结果

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;

将文件嵌入 C++ 语言

embin --lang=cpp data.png > output.hpp

结果

#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
};

将文件嵌入 Python 语言

embin --lang=python data.png > output.py

结果

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
])

⚠️ 在 Windows 上,生成嵌入资产时使用 --output 而不是 >,以避免运行 Python 代码时出现以下错误

源代码字符串不能包含空字节

嵌入多个文件

embin --lang=cpp assets/icon.png assets/banner.png > assets.hpp

结果

#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
};

选项

--format 设置生成的源代码格式

Char 格式

embin --format=char data.txt
const 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 格式

embin --format=octal data.txt
const 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 设置生成的源代码的缩进类型

使用制表符而不是空格进行缩进

embin --indent=tab data.png

--padding 设置生成的源代码的填充值

embin --padding 0 data.png
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;

--quantity 设置每行的字节数量

embin --quantity 8 data.png
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;

--mutable 使生成的变量可变

embin --mutable data.png
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
};
unsigned int data_png_len = 42;

许可证

本项目采用 MIT 许可证发布。

贡献

欢迎提交拉取请求。对于主要更改,请先提交问题以讨论您想要更改的内容。

依赖项

~1–11MB
~74K SLoC