You can't mix them like that. What you can do is to insert hex codes withing a string, such as with:
char T[] = "\x10\x0f\xf0" "Hello" "\xc1\xd0";
Keep in mind this will put a \0
at the end of that array so you may need to adjust for that if you're using sizeof
to get the byte count:
size_t bytesInT = sizeof(T) - 1;
Similarly, there won't be a null at the end of the Hello
but you can fix that with:
char T[] = "\x10\x0f\xf0" "Hello\0" "\xc1\xd0";
You may wonder why the string is separated into three components. This is valid because a very early stage of the compilation process involves concatenating adjacent strings, meaning that "paxdiablo"
and "pax" "diablo"
are functionally equivalent.
One reason is for readability, but the important one is to ensure that the hex specifications don't "swallow" the following characters. It won't matter for Hello
since the H
is not a valid hex character but you can see the problem here:
#include <stdio.h>
int main(void) {
printf ("%d %d\n", sizeof("\x20Hi"), sizeof("\x20Bi"));
return 0;
}
This will output 4 3
despite the only difference (seemingly) in the two strings being a change of one character. The lengths are different because the B
in Bi
is actually consumed as a hex digit, being treated as \x20B
(although gcc
, for one, will warn you that the resultant value is out of range).