Convert any file into a C++ array header file (.h) using Python.
- Convert any file (binary or text) into a C++ array.
- Optional chunking for very large files.
- Generates a single header file (
.h) with:- All chunks combined in a single array
- Array length
- Optional helper functions
- Automatic safe array name generation from file names.
- Python 3.6+
No external packages are required.
python file2cpp.py -i <input_file> [-o <output_header>] [-n <array_name>] [-chunk <size>]-i/--input: Input file path (required)-o/--output: Output header file (default:output.h)-n/--name: Array name (optional; generated from file name if empty)-chunk: Chunk size in bytes for splitting large files (default: 0 = no chunking)
Convert example.png into a C++ header:
python file2cpp.py -i example.pngGenerated output.h:
#pragma once
#include <vector>
#include <cstring>
size_t example_pngLen = 532;
unsigned char example_png[] = {
0x23, 0x20, 0x49, 0x66, 0x20, ...
};
inline std::vector<unsigned char> getexample_pngVector() {
std::vector<unsigned char> result;
result.insert(result.end(), example_png, example_png + sizeof(example_png));
return result;
}
OR :
python file2cpp.py -i example.png -o example.h -n example -chunk 500Generated example.h:
#pragma once
#include <vector>
#include <cstring>
size_t exampleLen = 532;
unsigned char example_0[] = {
0x23, 0x20, 0x49, 0x66, 0x20, ...
};
unsigned char example_1[] = {
0x6F, 0x2E, 0x77, 0x6F, ...
};
inline std::vector<unsigned char> getexampleVector() {
std::vector<unsigned char> result;
result.insert(result.end(), example_0, example_0 + sizeof(example_0));
result.insert(result.end(), example_1, example_1 + sizeof(example_1));
return result;
}
inline unsigned char* getexampleBytes() {
static unsigned char combined[532];
size_t offset = 0;
memcpy(combined + offset, example_0, sizeof(example_0)); offset += sizeof(example_0);
memcpy(combined + offset, example_1, sizeof(example_1)); offset += sizeof(example_1);
return combined;
}
Include this header in any C++ project to access file contents directly.
- Chunking is optional; if not used, a single array is generated.
- Array names are sanitized to valid C++ identifiers.
- Works across multiple C++ source files without duplication issues.