🌐
GitHub
github.com › bschauerte › xxd › blob › master › xxd.m
xxd/xxd.m at master · bschauerte/xxd
% XXD provides an array data export interface to C/C++ by generating a · % complete static C/C++ array definition (similar to xxd -i on Unix). % % If you want to implement the output for another datatype, then just · % ...
Author: bschauerte
🌐
Johannst
johannst.github.io › notes › binary › xxd.html
xxd - Notes
xxd [opts] -p dump continuous hexdump -r convert hexdump into binary ('revert') -e dump as little endian mode -i output as C array
🌐
Csuohio
grail.eecs.csuohio.edu › ~somos › xxd.c
xxd.c V1.10 27oct98 by Juergen Weigert (updated 2013)
*/ #if defined(sun) && defined(FILE) && !defined(__SVR4) && defined(__STDC__) # define __P(a) a /* excerpt from my sun_stdlib.h */ extern int fprintf __P((FILE *, char *, ...)); extern int fputs __P((char *, FILE *)); extern int _flsbuf __P((unsigned char, FILE *)); extern int _filbuf __P((FILE *)); extern int fflush __P((FILE *)); extern int fclose __P((FILE *)); extern int fseek __P((FILE *, long, int)); extern int rewind __P((FILE *)); extern void perror __P((char *)); # endif #endif extern long int strtol(); extern long int ftell(); char version[] = "xxd V1.10 27oct98 by Juergen Weigert"; #ifdef WIN32 char osver[] = " (Win32)"; #else # ifdef DJGPP char osver[] = " (dos 32 bit)"; # else # ifdef MSDOS char osver[] = " (dos 16 bit)"; # else char osver[] = ""; # endif # endif #endif #if defined(MSDOS) || defined(WIN32) || defined(OS2) # define BIN_READ(yes) ((yes) ?
🌐
Matgomes
matgomes.com › home › cpp › converting files to c++ byte arrays
Converting Files To C++ Byte Arrays - Matheus Gomes
July 9, 2022 - In particular, xxd is essentially a tool for making (or reversing) hexadecimal dumps. Specifically, thexxd -i FILE command will turn the file path FILE into a c-style array.
🌐
GitHub
github.com › chettrick › xxdc
GitHub - chettrick/xxdc: hexadecimal dump to C include file · GitHub
The xxdc utility reads infile and writes a hexadecimal static array definition and array length to the standard output in a form suitable for inclusion in a C program.
Author: chettrick
🌐
Csuohio
grail.cba.csuohio.edu › ~somos › xxd.c
https://grail.cba.csuohio.edu/~somos/xxd.c
*/ #if defined(sun) && defined(FILE) && !defined(__SVR4) && defined(__STDC__) # define __P(a) a /* excerpt from my sun_stdlib.h */ extern int fprintf __P((FILE *, char *, ...)); extern int fputs __P((char *, FILE *)); extern int _flsbuf __P((unsigned char, FILE *)); extern int _filbuf __P((FILE *)); extern int fflush __P((FILE *)); extern int fclose __P((FILE *)); extern int fseek __P((FILE *, long, int)); extern int rewind __P((FILE *)); extern void perror __P((char *)); # endif #endif extern long int strtol(); extern long int ftell(); char version[] = "xxd V1.10 27oct98 by Juergen Weigert"; #ifdef WIN32 char osver[] = " (Win32)"; #else # ifdef DJGPP char osver[] = " (dos 32 bit)"; # else # ifdef MSDOS char osver[] = " (dos 16 bit)"; # else char osver[] = ""; # endif # endif #endif #if defined(MSDOS) || defined(WIN32) || defined(OS2) # define BIN_READ(yes) ((yes) ?
🌐
GitHub
gist.github.com › 3621010
hexdump convert binary to c array data · GitHub
hexdump convert binary to c array data. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
CMake Discourse
discourse.cmake.org › development
Support for embedding data in a manner equivalent to xxd - Development - CMake Discourse
November 4, 2020 - The xxd command can convert any input file to a form which can be included into a C source file or compiled directly: % echo "Sample data" > test % xxd -i test unsigned char test[] = { 0x53, 0x61, 0x6d, 0x70, 0x6c, 0…
🌐
GitHub
github.com › vim › vim › issues › 13876
xxd Code Generation is not compatible with C standard for large arrays · Issue #13876 · vim/vim
January 17, 2024 - Steps to reproduce The option -i is supposed to generate an array length variable as an int. The C standard only guarantees 16 bits for an int. An array greater than 2^16 entries should cause issue...
Author: vim
🌐
CoCalc
cocalc.com › github › vim › vim › blob › master › src › xxd › xxd.c
CoCalc -- xxd.c
11 * 1.11.95 if "xxd -i" knows the filename, an 'unsigned char filename_bits[]' 12 * array is written in correct c-syntax. 13 * -s improved, now defaults to absolute seek, relative requires a '+'. 14 * -r improved, now -r -s -0x... is supported. 15 * change/suppress leading '\0' bytes.
Top answer
1 of 6
87

xxd has a mode for this. The -i/--include option will:

output in C include file style. A complete static array definition is written (named after the input file), unless xxd reads from stdin.

You can dump that into a file to be #included, and then just access foo like any other character array (or link it in). It also includes a declaration of the length of the array.

The output is wrapped to 80 bytes and looks essentially like what you might write by hand:

$ xxd --include foo
unsigned char foo[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
  0x21, 0x0a, 0x0a, 0x59, 0x6f, 0x75, 0x27, 0x72, 0x65, 0x20, 0x76, 0x65,
  0x72, 0x79, 0x20, 0x63, 0x75, 0x72, 0x69, 0x6f, 0x75, 0x73, 0x21, 0x20,
  0x57, 0x65, 0x6c, 0x6c, 0x20, 0x64, 0x6f, 0x6e, 0x65, 0x2e, 0x0a
};
unsigned int foo_len = 47;

xxd is, somewhat oddly, part of the vim distribution, so you likely have it already. If not, that's where you get it — you can also build the tool on its own out of the vim source.

2 of 6
14

You can almost do what you want with hexdump, but I can't figure out how to get quotes & single backslashes into the format string. So I do a little post-processing with sed. As a bonus, I've also indented each line by 4 spaces. :)

hexdump -e '16/1 "_x%02X" "\n"' filename | sed 's/_/\\/g; s/.*/    "&"/'

Edit

As Cengiz Can pointed out, the above command line doesn't cope well with short data lines. So here's a new improved version:

hexdump -e '16/1 "_x%02X" "\n"' filename | sed 's/_/\\/g; s/\\x  //g; s/.*/    "&"/'

As Malvineous mentions in the comments, we also need to pass the -v verbose option to hexdump to prevent it from abbreviating long runs of identical bytes to *.

hexdump -v -e '16/1 "_x%02X" "\n"' filename | sed 's/_/\\/g; s/\\x  //g; s/.*/    "&"/'
🌐
Google AI
ai.google.dev › google ai edge › build and convert models
Build and convert models | Google AI Edge | Google AI for Developers
December 5, 2025 - The following unix command will generate a C source file that contains the LiteRT model as a char array: xxd -i converted_model.tflite > model_data.cc ·
🌐
GitHub
github.com › vim › vim › blob › master › src › xxd › xxd.c
vim/src/xxd/xxd.c at master · vim/vim
* 1.11.95 if "xxd -i" knows the filename, an 'unsigned char filename_bits[]' * array is written in correct c-syntax. * -s improved, now defaults to absolute seek, relative requires a '+'. * -r improved, now -r -s -0x... is supported. * change/suppress leading '\0' bytes.
Author: vim
🌐
Readthedocs
extfs.readthedocs.io › tools › arraydump.html
arraydump — extfs 1.0.0 documentation
Young project: arraydump is a very young tool. Because of this, it has not seen a lot of use outside the extfs project. This means that there are probably bugs that have not yet surfaced and might cause wrong output to be produced. If you find any bugs, please do not hessitate to report them, or even better create a pull request. Written in Python: In contrast to xxd, which is written in C, arraydump is written in Python.
🌐
Lua Ternary
nachtimwald.com › tags › xxd
Xxd | John's Blog
Introduction Once again I needed ... it’s the most portable way because some compilers have string length limitations. xxd -i is the easiest way to format the data so it can be compiled in....
🌐
MathWorks
in.mathworks.com › matlabcentral › fileexchange › 44112-xxd-hexdump
xxd (hexdump) - File Exchange - MATLAB Central
October 29, 2013 - xxd provides a data export interface to C/C++ headers by generating a static C/C++ array definition
🌐
GitHub
github.com › build2-packaging › xxd › blob › master › xxd › README.md
xxd/xxd/README.md at master · build2-packaging/xxd
It creates a hexdump of a given file or standard input. It can also convert a hexdump back to its original binary form. One notable xxd mode is -i|--include, which generates a C array definition of the input that can be used to embed binary ...
Author: build2-packaging
🌐
Université catholique de Louvain
sites.uclouvain.be › SystInfo › manpages › man1 › xxd.1.html
Man page of XXD
output in C include file style. A complete static array definition is written (named after the input file), unless xxd reads from stdin.