1

I have const binary data that I need insert to buffer for example

 char buf[] = "1232\0x1";

but how can do it when binary data is at first like below

 char buf[] = "\0x11232";

compiler see it like a big hex number but my perpose is

 char buf[] = {0x1,'1','2','3','2'};
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49

3 Answers3

4

You can use compile-time string concatenation:

char buf[] = "\x01" "1232";

However, with a 2-digit number after \x it also works without:

char buf[] = "\x011232";

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    This is incorrect. The escape sequence will consume more than two characters in the second example. See http://stackoverflow.com/q/5784969/163956 – Greg Inozemtsev May 15 '12 at 07:28
3

You can create a single string literal by composing it of adjacent strings - the compiler will concatenate them:

char buf[] = "\x1" "1232";

is equivalent to:

char buf[] = {0x1,'1','2','3','2', 0};  // note the terminating null, which may or may not be important to you
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
1

You have to write it in two byte or four byte format:

\xhh = ASCII character in hexadecimal notation

\xhhhh = Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.

so in your case you have to write "\x0112345"

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115