Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert binary data into hex string for C program

I have a small binary image, that needs to be represented in a C program. The representation will be like:

static const char[] = {0x1, 0x2, 0x3, 0x4...};

(so, the bytes will be represented as a series of chars)

How do I convert a binary file into a nice 0x..,0x.. string? Is there a program to do this?

like image 632
Lucky5 Avatar asked Jan 20 '26 17:01

Lucky5


1 Answers

Use the xxd utility:

$ xxd -i binary.bin > binary.c

Will create binary.c with the following content:

unsigned char binary_bin[] = {
  0x01, 0x02, 0x03, 0x04,   . . .
  . . .
};
unsigned int binary_bin_len = 123456;
like image 139
rustyx Avatar answered Jan 23 '26 09:01

rustyx