John Doe
John Doe

Reputation: 460

Hex in char array

If the following is a one byte array:

char arr[] = "\xFF";

If we do the following:

char arr[] = "\xFFmyrandomstringappendedafterbyte";

printing it would result in this:

byteValueGoesHEREmyrandomstringappendedafterbyte

However, if I try to do the following:

char arr[] = "\xFF98";

It will result in a warning:

warning: hex escape sequence out of range

It treats 98 as part of the hexcode. However, I would it to be treated as a string (as is myrandomstringappendedafterbyte).

I would like to have this as output byteValueGoesHERE98.

How can this be achieved without a whitespace? How can I denote that 98 should be treated as a string?

Upvotes: 2

Views: 125

Answers (1)

aschepler
aschepler

Reputation: 72483

When string literals have only whitespace (or nothing) between them, the preprocessor combines them into a single string literal, but this does not "merge" escape sequences. So you can just write your desired string using two strings:

char arr[] = "\xFF" "98";

This is four bytes including the terminating '\0'.

Upvotes: 3

Related Questions