Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the Length of a Hexadecimal Escape Sequence in a C-String [duplicate]

Tags:

c

string

In a string literal, how can I follow a hexadecimal escape sequence immediately with a literal character that can be interpreted as a hexadecimal digit? For example, if I write this literal ...

"BlahBlah\x04BlahBlah"

... the 'B' immediately following the '4' will be interpreted as part of the hexadecimal escape, because it is a valid hexadecimal digit.

How can I write a string literal that represents the string that the above would represent if the '4' were taken as the last character of the hex escape?

like image 738
wahab Avatar asked Feb 03 '16 15:02

wahab


1 Answers

As you noticed, C is pretty dumb when it comes to hex escape sequences in string literals. Fix it by using string concatenation, like this:

"BlahBlah\x04" "BlahBlah"

It is good practice to never have any trailing characters behind such a hex escape sequence. Always end the string as in this example.

like image 81
Lundin Avatar answered Nov 13 '22 07:11

Lundin