Home › Developer Tools › File to C Array

File to C Array

Turn any file into a C or C++ byte array you can compile straight into firmware or an application, just like xxd -i. Choose hex or decimal, the bytes per line, the element type, and add const, static, PROGMEM, alignment or a header guard.

Last updated:

Offset and count accept decimal or 0x hex.

Advertisement
Advertisement

How to use the File to C Array

  1. Drop a file (or switch to the Text tab and type a string).
  2. Check the variable name; it is made from the file name, with dots, dashes and spaces turned into underscores.
  3. Pick the format: element type, hex or decimal values and bytes per line.
  4. Add qualifiers such as const, static or PROGMEM, and choose how the length is exported.
  5. Copy the code or download the .h file and include it in your project.

Example output

For a 23-byte text file named hello.txt, the default settings produce (untick const to get exactly what xxd -i prints):

/* hello.txt (23 bytes) */
const unsigned char hello_txt[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x65, 0x6d, 0x62, 0x65, 0x64,
  0x64, 0x65, 0x64, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a
};
const unsigned int hello_txt_len = 23;

Use it like any other array: fwrite(hello_txt, 1, hello_txt_len, f); or, for a PROGMEM array on AVR, pgm_read_byte(&hello_txt[i]).

Options compared

OptionWhat it addsWhen to use it
constconst unsigned char name[]Almost always: read-only data can stay in flash or a read-only section.
staticstatic linkageWhen the header is included in several .c files, to avoid duplicate symbol errors.
PROGMEMPROGMEM + #include <avr/pgmspace.h>AVR boards (Uno, Nano, Mega) to keep data out of SRAM.
Align to 4 bytes__attribute__((aligned(4))) or alignas(4)DMA buffers, or reading the bytes as 32-bit words.
Header guard#ifndef NAME_H … #endifWhen the file is saved and included as a .h header.
0x00 terminatorOne extra zero byteEmbedding text that will be used as a C string.
C++ std::arrayconstexpr std::array<T, N>Modern C++ code that wants the size in the type and compile-time access.

Alternatives to a byte array

  • C23 #embed: newer compilers (GCC 15, Clang 19) support #embed "file.bin" inside an initializer, which reads the file at compile time.
  • Linker objects: ld -r -b binary file.bin -o file.o or objcopy turn a file into an object with _binary_file_bin_start and _end symbols.
  • Byte arrays (this tool) remain the most portable choice: they work with every compiler, IDE and microcontroller toolchain and need no build-system changes.

Frequently asked questions

Is the output the same as xxd -i?

Yes, apart from a one-line comment naming the file. Untick const and keep the other defaults (C array, unsigned char, lowercase hex, 12 bytes per line, unsigned int length) and the layout matches xxd -i: an unsigned char array named after the file, two-space indentation, and an unsigned int name_len variable holding the byte count.

How is the variable name made from the file name?

Every character that is not a letter, digit or underscore becomes an underscore, so logo.png becomes logo_png and font-16px.bin becomes font_16px_bin. A name that would start with a digit gets a data_ prefix, because C identifiers cannot begin with a number. You can type any other name.

What does PROGMEM do?

On AVR microcontrollers such as the Arduino Uno, const data is still copied into the small SRAM at startup. Adding PROGMEM (from avr/pgmspace.h) keeps the array in flash memory instead, and you read it with pgm_read_byte(). On ESP32, ARM and most other chips, const alone already keeps data in flash.

Should I use unsigned char or char?

Use unsigned char or uint8_t for binary data. Plain char is signed on most compilers, so values above 0x7F do not fit, which is a narrowing error in C++ brace initialisation and a warning in C. If you choose char or signed char, those bytes are written with an explicit cast.

Why add a 0x00 terminator?

Appending a zero byte lets you use an embedded text file (HTML, JSON, a shader or a certificate) directly as a C string. The length value then includes that extra byte.

How large a file can I convert?

Files up to 32 MB can be converted in the browser. The text output is roughly six times the file size, so very large outputs are only previewed partly on screen, but copying and downloading always give the full header.

Is my file uploaded?

No. The file is read with the browser's File API and the header is generated on your device.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

Image to C ArrayConvert images to monochrome, RGB565 or RGB888 C arrays for OLED, LCD and TFT displays.File to Base64Convert any file to a Base64 string or data URI with MIME detection, line wrap and download.Online Hex EditorOpen any file as hex and ASCII, edit bytes, search, inspect values and save the result.CRC32 Checksum CalculatorCalculate CRC-32, CRC-32C, CRC-16 and Adler-32 checksums of text or large files.JSON to Excel ConverterTurn JSON into a styled .xlsx spreadsheet; nested arrays become their own linked sheets.JSON to CSVConvert JSON data to CSV for spreadsheets.CSV to JSONConvert CSV data to JSON with live preview.Excel to CSVConvert XLSX spreadsheets to CSV.CSV ViewerOpen a CSV or TSV file as a sortable, filterable table.Excel Viewer (XLSX / XLS)Open XLSX, XLS and ODS spreadsheets in your browser without Excel.Line Ending ConverterConvert text and files between CRLF, LF and CR line endings.Markdown to HTMLConvert Markdown to clean HTML with a live editor.XML to JSONConvert XML to JSON with validation.Text Encoding ConverterDetect and convert text file encodings like UTF-8, UTF-16 and Latin-1.JSON ViewerExplore JSON as a collapsible tree, search keys and values and copy any node's path.TTF to WOFF2 ConverterConvert TTF and OTF fonts to WOFF2 or WOFF and get a ready @font-face CSS snippet.Chmod CalculatorConvert Linux file permissions between checkboxes, octal (755) and symbolic (rwxr-xr-x) notation.JSON to TypeScriptGenerate TypeScript interfaces or types from JSON, with nested types and optional fields.Morse Code TranslatorTranslate text to Morse code and back, then play it as sound or a blinking light, or save a WAV.HTML Table GeneratorBuild HTML tables in an editable grid with styles, header rows, CSV import and live preview.Hex to Text ConverterConvert hex to text and text to hex with UTF-8 support and space, 0x, \x or comma formats.JSON to XML ConverterConvert JSON to well-formed XML with attributes, arrays and pretty print.JSON to YAML ConverterConvert JSON to clean YAML as you type, with indent, quoting and sort options.YAML to JSON ConverterConvert YAML to pretty or minified JSON, with errors pinned to the exact line.HTML to Markdown ConverterTurn HTML or pasted rich text into clean Markdown with tables and live preview.Markdown Table GeneratorBuild Markdown tables in a grid or paste from Excel/CSV, with alignment and preview.JSON ValidatorCheck JSON with the exact line and column of each error, fix tips and one-click auto-fix.Image to Base64Convert images to Base64 data URIs, CSS backgrounds and HTML img tags in your browser.Base64 to ImagePaste a Base64 string or data URI to preview, identify and download the image.JSON CompareCompare two JSON documents and see every added, removed and changed path.Hex to DecimalConvert hex to decimal instantly with steps and signed mode.Decimal to HexConvert decimal to hex instantly with steps and signed mode.Binary to DecimalConvert binary to decimal instantly with steps and signed mode.Decimal to BinaryConvert decimal to binary instantly with steps and signed mode.Two's Complement CalculatorDecimal to two's complement binary and back for 4-64 bits, with invert-and-add-1 steps.XML to CSV ConverterConvert XML to CSV: detects repeating records, flattens nested data, pick your columns.CSV to XML ConverterConvert CSV to well-formed XML with custom root and row names, escaping and pretty print.TOML to JSON ConverterConvert TOML to JSON and JSON to TOML with a full v1.0 parser and clear error lines.ES6 to ES5 ConverterTranspile modern JavaScript (ES2015+) to ES5 with Babel: targets, module format and loose mode.JavaScript Object to JSONTurn JS object literals and Python dicts into valid JSON: quotes keys, fixes commas and values.JSON Graph VisualizerSee JSON as an interactive node graph with zoom, collapse and search; export PNG or SVG.KML ViewerOpen KML and KMZ files on an interactive map: placemarks, paths, polygons, folders, export.MySQL to PostgreSQL ConverterConvert MySQL dumps and CREATE TABLE statements to PostgreSQL: types, AUTO_INCREMENT, ENUMs, quotes.Text to CSV ConverterTurn space-aligned, fixed-width, pipe or tab separated text into clean CSV with a live preview.VCF File ViewerOpen vCard .vcf files as contact cards with photos, search and duplicate check, export CSV.XML ViewerBrowse XML as a collapsible tree with search, XPath of any node, attributes and big-file support.JSON to Go StructTurn JSON into Go structs with inferred types, nested structs and json tags.JSON to Python ClassGenerate Python dataclasses, Pydantic models or TypedDicts with type hints from JSON.JSON to C# ClassConvert JSON to C# classes or records with System.Text.Json or Newtonsoft attributes.JSON SorterSort JSON keys A-Z recursively, sort arrays by value or by a key, then pretty-print or minify.JSON to HTML TableConvert a JSON array of objects into clean HTML table code with CSS styles and a live preview.CSV to SQL ConverterConvert CSV to SQL CREATE TABLE and INSERT statements for MySQL, PostgreSQL, SQLite or SQL Server.IPv4 to IPv6 ConverterConvert IPv4 to IPv4-mapped, 6to4 and NAT64 IPv6, extract IPv4 from IPv6, and get int/hex/binary.cURL to Code ConverterConvert curl commands to JavaScript fetch, Axios, Python requests, Node.js, PHP and Go code.Code to ImageTurn code into a beautiful syntax-highlighted PNG with themes, window frame and background.Excel to JSON ConverterConvert Excel sheets (XLSX, XLS, ODS) to JSON with header row, type and shape options.CSV to Excel ConverterConvert CSV files to a real .xlsx workbook with typed numbers and dates, one sheet per file.CSV SplitterSplit a large CSV into smaller files by row count, file count or column value, header kept.CSV MergerCombine many CSV files into one, matching columns by name, with dedupe and a source column.VCF to CSV ConverterConvert vCard (.vcf) contacts to CSV for Google or Outlook, or turn a CSV back into a VCF.Calendar Event (.ics) GeneratorCreate an .ics calendar invite with time zone, reminders and repeats + Google/Outlook links.CSV Column ExtractorPick, reorder and rename CSV columns, filter rows by value, and export a new CSV or JSON.CSV Diff CheckerCompare two CSV files by key column and see added, removed and changed rows cell by cell.CSV Duplicate Row RemoverRemove duplicate rows from a CSV by all or selected columns and download the cleaned file.CSV to HTML TableTurn CSV into clean HTML table code with a live preview, styling options and one-click copy.CSV to Markdown TableConvert CSV to a neatly aligned GitHub Markdown table with column alignment and live preview.CSV TransposeSwap rows and columns of CSV, TSV or pasted spreadsheet data, with delimiter auto-detect.Date Format ConverterConvert any date to ISO 8601, RFC 2822, Unix time and custom strftime, moment or Java patterns.
See all 129 Data & File Format Converters →