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.

Answer from Michael Homer on Stack Exchange
Starred by 10 users
Forked by 4 users
Languages: Python 63.3% | Lua 36.7%
🌐
Lua Ternary
nachtimwald.com › posts › binary headers
Binary Headers | John's Blog
October 23, 2015 - IN NO EVENT SHALL THE # AUTHORS ... re import struct import sys ''' Output a binary file that was created with xxd -i. xxd cannot reverse its output when using the -i (C header file output)....
🌐
GitHub
github.com › fluent › fluent-bit › blob › master › tools › xxd-c › xxd-c.c
fluent-bit/tools/xxd-c/xxd-c.c at master · fluent/fluent-bit
/* Pre-processor header */ len = snprintf(tmp, sizeof(tmp) - 1, "#ifndef XXD_C_%s\n" "#define XXD_C_%s\n\n", sname, sname); fwrite(tmp, len, 1, f_out); · /* Define byte array */ len = snprintf(tmp, sizeof(tmp) - 1, "static unsigned char __%s[] = {", sname); fwrite(tmp, len, 1, f_out); ·
Author: fluent
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/.*/    "&"/'
🌐
Ninimotom
ninimotom.com › notebook › xxd
Use xxd to create C headers - Nini Mo Tom
Mostly people use it to inspect hexdumps from binaries, but it has a surprising amount of functionality above and beyond that: it can revert hexdumps to binary, it can patch hexdump output back into an existing binary file, and (the subject of this note) it can format its output as a C header file.
🌐
GitHub
github.com › vim › vim › issues › 14409
xxd feature request - option to add a \0 (0x00) character at the end of the data when generating a C header · Issue #14409 · vim/vim
April 4, 2024 - When creating a C header file with xxd from a file, it may sometimes be a piece of text you want to include. It would be handy if it can be used directly as a c-string which would need to be zero t...
Author: vim
Find elsewhere
🌐
GitHub
github.com › landley › toybox › blob › master › toys › other › xxd.c
toybox/toys/other/xxd.c at master · landley/toybox
int c = 1, i, len; · // The original xxd outputs a header/footer if given a filename (not stdin). // We don't, which means that unlike the original we can implement -ri. while ((len = read(fd, toybuf, sizeof(toybuf))) > 0) { for ...
Author: landley
🌐
Lua Ternary
nachtimwald.com › posts › python binary to c header
Python Binary to C Header | John's Blog
October 9, 2019 - 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....
🌐
ResearchGate
researchgate.net › figure › An-Example-of-Extracting-a-PDF-Header-Using-the-UNIX-xxd-Command_fig6_277622277
An Example of Extracting a PDF Header Using the UNIX xxd Command. | Download Scientific Diagram
... is one line that identifies the version number of the PDF specifications used in the document. The UNIX xxd command (as shown in Figure 3) can be used to read the header.
🌐
thelinuxvault
thelinuxvault.net › blog › linux-xxd-command-explained
Linux xxd Command Explained
By default, xxd uses lowercase ... 00100001 World! 0000000c: 00001010 . The -i (include) option outputs the hex dump as a C header file, with a byte array and length variable....
🌐
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…
🌐
Mitjafelicijan
mitjafelicijan.com › embedding-resources-into-binary-with-c.html
Embedding resources into binary with C
June 19, 2024 - Convert text.txt into a C header file with xxd -i test.txt > test.h.
🌐
Blogger
falsinsoft.blogspot.com › 2015 › 03 › linux-tool-convert-binary-file-to-cc.html
Linux tool: convert binary file to C/C++ source code array
November 21, 2015 - The use is very simple, if you have a binary file called, for example, MyFile.bin with length of 100 bytes and wan to convert in a C/C++ source code array you can simply type: ... Obviously 0xXX is the hexadecimal content of the binary file.
🌐
Discourse
fortran-lang.discourse.group › uncategorized
Binary data mischief with od-xxd-hexdump and fortran - Uncategorized - Fortran Discourse
May 10, 2022 - Hi Forts! I am a longtime lurker and first time poster. This is something I am writing for my blog. It is not quite complete but I felt like sharing it here. Hope its not too basic for this crowd! There is a mysterious file with a magic number binary header. It is not known how this file was ...