Dave's Brain

Browse - Programming Tips - How can I easily include a binary file in my code?

Date: 2016jan2
Language: C/C++

Q.  How can I easily include a binary file in my code?

A.  Use the xxd command with --include option:

xxd --include example.data
When file example.data contains "example\n" you get:
unsigned char example_data[] = { 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x0a }; unsigned int example_data_len = 8;
So, of course you can do:
xxd --include example.data > example.cpp
Perhaps in a Makefile

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.