Bin2C (Binary to C) is a lightweight, command-line utility used to convert any asset file into a C/C++ byte array so it can be compiled directly into an executable. This eliminates the need to distribute external files or read assets from a disk at runtime. Why Developers Use Bin2C
Self-Contained Executables: Perfect for bundling fonts, shaders, audio clips, or UI textures directly into a single program binary.
Embedded & Firmware Engineering: Widely used in environments like SEGGER Embedded Studio to load assets directly from microcontroller flash memory without an underlying file system.
Bypassing File I/O: Eliminates disk read latency and missing-file crashes. Standard Workflow Tutorial Step 1: Prepare Your Asset
Assume you have a small user-interface image named logo.png that you want to integrate into your application. Step 2: Run the Bin2C Command
Depending on the specific variant of the tool you download (such as rkitover/bin2c or megastep/bin2c on GitHub), the syntax is straightforward: bin2c -i logo.png -o logo.h -a logo_data Use code with caution. -i logo.png: Specifies your input resource file. -o logo.h: Dictates the output C header file name.
-a logo_data: Assigns the variable name for the generated array. Step 3: Inspect the Output
The utility creates a text file containing the hexadecimal breakdown of your image:
/Generated automatically by bin2c */ const unsigned char logo_data[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0xD4, // … hundreds of bytes of hex data … 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 }; const unsigned int logo_data_length = 12450; Use code with caution. Step 4: Include the Header in Your Code
You can now reference the array directly from your C or C++ application source code without calling traditional file reading handlers:
#include Use code with caution. Common Alternatives to Bin2C
If you do not want to install a standalone tool, you can leverage native OS alternatives:
Linux & macOS (xxd): Virtually all Unix-like systems have xxd built in. Running xxd -i logo.png > logo.h yields an identical C array format automatically.
Modern C++20 (std::embed) / C23 (#embed): Modern compiler specifications have introduced native syntax directives (like #embed “logo.png”) to achieve this natively without third-party translation scripts. Security Caution
Embedding raw byte arrays stores assets explicitly inside the compiled binary. Never use Bin2C for sensitive details like API private keys, server passwords, or encryption seeds, as anyone can extract them using basic decompilation or string-dump tools.
Are you working on an embedded system firmware or a desktop application? Let me know your development environment so I can provide the exact tool flags or a modern C++ wrapper snippet! GitHub – rkitover/bin2c: convert file data to C byte array
Repository files navigation. README. BSD-2-Clause license. NAME. bin2c – convert file data to C byte array. SYNOPSIS. bin2c [ -h,- Convert Files To Hexadecimal Byte Arrays for C / C++
Leave a Reply