Embedding Binary Data in C/C++ Code (xxd -i)

While the primary purpose of xxd is to create hexadecimal dumps, xxd -i takes it a step further by generating a C include file that contains the binary data as an array.

xxd -i [input file] > [output.c]

The generated output.c file includes an array with the binary data from hello.txt. For example, you might get something like this in output.c:

Output: xxd -i hello.txt

  • unsigned char hello_txt[] is an array of unsigned characters, which represents the binary data of hello.txt.
  • The array is initialized with a list of hexadecimal values enclosed within curly braces. Each hexadecimal value represents a byte of data, with the 0x prefix denoting a hexadecimal number. The values correspond to the ASCII codes of the characters in “hello.txt.”
  • unsigned int hello_txt_len is an additional variable that stores the length of the array. In this case, it’s set to 30, indicating that the array contains 30 bytes of data.

Now, you can easily use this array in your C or C++ code, making it convenient for embedding binary data, such as images or other resources, directly into your applications.

C program to utilize the output of xxd -i:

You can use the generated C code to display the text from hello.txt as follows:

C
#include <stdio.h>

// The generated data
unsigned char hello_txt[]
    = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x68,
        0x6f, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79,
        0x6f, 0x75, 0x3f, 0x0a, 0x49, 0x27, 0x6d, 0x20,
        0x66, 0x69, 0x6e, 0x65, 0x21, 0x0a };

unsigned int hello_txt_len = 30;

int main()
{
    // Print the content of hello.txt
    for (unsigned int i = 0; i < hello_txt_len; i++) {
        putchar(hello_txt[i]);
    }

    return 0;
}

Output
Hello, how are you?
I'm fine!


This C code includes the generated data array hello_txt and its length hello_txt_len. The program iterates through the array and uses the putchar function to display the data, printing the original content of “hello.txt.”

xxd Command in Linux

xxd is a command-line tool that is primarily used for creating and analyzing hexadecimal dumps from files. It can also be used to reverse the process and convert a hexadecimal dump back into binary form. In this article, let’s uncover the practical applications of the “xxd” command in the Linux ecosystem.

xxd Command in Linux

  • What is a ‘hexadecimal dump’?
  • Installing xxd on Linux
  • Using xxd command:
  • Generating hexadecimal dumps
  • Converting hexadecimal dump to the original file
  • Formatting output of xxd
  • Different dump output styles
  • Embedding Binary Data in C/C++ Code (xxd -i)

Similar Reads

What is a ‘hexadecimal dump’?

A hexadecimal dump, often called a hex dump, is a representation of binary data in a human-readable format using hexadecimal notation. Each byte of binary data is displayed as a pair of hexadecimal digits, making it easier to understand the data....

Installing xxd on Linux

While most Linux distributions come with the “xxd” command pre-installed, if you encounter an error when attempting to use it, you may need to install it based on your specific distribution....

Using xxd command:

Syntax:...

Generating hexadecimal dumps

To create a hexadecimal dump of a file, you can use the following command:...

Converting hexadecimal dump to the original file

xxd -r [hexadecimal dump file path]...

Formatting output of xxd

Skipping n bytes from start (-s offset)...

Different dump output styles

Binary digit dump (-b)...

Embedding Binary Data in C/C++ Code (xxd -i)

While the primary purpose of xxd is to create hexadecimal dumps, xxd -i takes it a step further by generating a C include file that contains the binary data as an array....

xxd Command in linux -FAQs

What is the primary purpose of the “xxd” command in Linux?...

Conclusion

The “xxd” command in Linux is a versatile tool that allows users to work with binary data easily. Whether you need to analyze binary files, edit them with a text editor, or convert between hexadecimal dumps and binary data, “xxd” provides a valuable solution. Understanding and utilizing this command can be a significant asset for anyone dealing with binary data in the Linux environment....