Bin2Data is a command-line utility, written in QB64-PE, that converts binary files into source code, making it easy to embed resources directly into your executables. It can also optionally compress the data.
- Converts binary files into embeddable source code formats.
- Optionally compresses data using QB64-PE's
_DEFLATE$function. - Supports multiple output formats:
- QB64-PE
DATAstatements (.bifile). - QB64-PE
CONSTstring (.bifile). - C-style header with a
uint8_tarray (.hfile).
- QB64-PE
- Can also output a raw zlib/deflate compressed file (
.deflate). - Base64 encoding for QB64-PE
DATAandCONSTformats. - Command-line interface with support for wildcards for batch processing.
- Clone this repository:
git clone --recurse-submodules https://github.com/a740g/Bin2Data.git - Change into the directory:
cd Bin2Data - Open
Bin2Data.basin the QB64-PE IDE and pressF5to compile it.
You can run Bin2Data from your terminal. The help screen shows the available options.
Bin2Data: Converts binary files to QB64-PE data
Copyright (c) 2025 Samuel Gomes
https://github.com/a740g
Usage: Bin2Data [-w characters_per_data_line] [-i compression_level] [-d] [-c] [-p] [-r] [-s] [-o] [filespec]
-w: A number specifying the number of characters per data line. 8-4096 (default 112)
-i: A number specifying the compression level. 1-10
-d: Generate DATA (.bas; default)
-c: Generate a CONST (.bas; suitable for small files)
-p: Generate a C array (.h)
-r: Dump the raw compressed file (.deflate)
-s: Disable compression and store the file instead
-o: Overwrite output file if it exists
Note:
* Will create filespec.bi/.h/.deflate (based on the switches)
* Can bulk convert files using wildcards
* filespec can be a URL
* If filespec.bi/.h/.deflate exists, then it will not be overwritten (unless -o is specified)
* Character per line may be changed in CONST mode due to QB64's 500 line continuation limit
* C output is barebones. Use sizeof to get the array size
For QB64-PE, you need Base64.bas from the Toolbox64 library.
This is the default mode. It creates a .bi file containing a RESTORE label and DATA statements.
-
Generate the file:
Bin2Data my_asset.pngThis will createmy_asset.png.bi. -
In your main QB64-PE program:
' Your program logic here
' ...
' Load the resource.
' The label name is generated based on the filename and size.
' You can find the exact label name in the generated .bi file.
RESTORE data_my_asset_png_bi_12345
DIM buffer AS STRING
buffer = Base64_LoadResourceData
' Now 'buffer' contains the binary data of my_asset.png
' ...
' At the end of your main code, before SUBs and FUNCTIONs:
' Include the generated data file
'$INCLUDE:'my_asset.png.bi'
' At the bottom of you source code, after SUBs and FUNCTIONs:
' Include the library implementation (assuming you have cloned Toolbox64 in the include directory under your project directory)
'$INCLUDE:'include/Base64.bas'This mode is suitable for smaller files. It creates a .bi file containing CONST definitions.
-
Generate the file:
Bin2Data -c my_icon.icoThis will createmy_icon.ico.bi. -
In your main QB64-PE program:
' Include the generated constants file
'$INCLUDE:'my_icon.ico.bi'
' Your program logic here
' ...
' Load the resource using the generated CONSTs.
' The CONST names are based on the filename and size.
' Check the generated .bi file for the exact names.
DIM buffer AS STRING
buffer = Base64_LoadResourceString(DATA_MY_ICON_ICO_BI_123, SIZE_MY_ICON_ICO_BI_123, COMP_MY_ICON_ICO_BI_123)
' Now 'buffer' contains the binary data of my_icon.ico
' ...
' At the bottom of you source code, after SUBs and FUNCTIONs:
' Include the library implementation (assuming you have cloned Toolbox64 in the include directory under your project directory)
'$INCLUDE:'include/Base64.bas'When using the -p switch, Bin2Data generates a C header file (.h).
-
Generate the file:
Bin2Data -p my_data.binThis will createmy_data.bin.h. -
In your C/C++ code:
#include "my_data.bin.h"
#include <stdio.h>
#include <string.h>
int main() {
// The header defines symbols based on the filename and size, for example:
// - const uint8_t my_data_bin_h_567[] = {...}; (the data array)
// - #define SIZE_MY_DATA_BIN_H_567() 567 (original size)
// - #define COMP_MY_DATA_BIN_H_567() 1 (is compressed)
// - #define DATA_MY_DATA_BIN_H_567() ... (pointer to data)
// Note: If the data is compressed (COMP_...() is 1),
// you will need a zlib/deflate library to decompress it.
size_t dataSize = sizeof(my_data_bin_h_567);
printf("Original size: %d\n", SIZE_MY_DATA_BIN_H_567());
printf("Embedded size: %zu\n", dataSize);
printf("Is compressed: %d\n", COMP_MY_DATA_BIN_H_567());
// You can now use the my_data_bin_h_567 array.
return 0;
}- This tool requires a recent version of QB64-PE to build.
- When you clone a repository that contains submodules, you need to run
git submodule update --init --recursiveto fetch the submodule content.
- Icon by Umut Pulat
