I need to represent an arbitrary sequence of bytes as a C++ string literal, but in the typical case where it is mostly an ASCII string, keep it as readable as reasonably possible. The obvious way to do this is as a string literal with escape sequences where necessary, but this runs into a problem.
Consider "\xaaa"
. The \x
escape sequence can take at most two hexadecimal digits. I would therefore expect the compiler to take those two hexadecimal digits, consider the escape sequence to be complete, and go back to reading literal characters. Instead, Microsoft C++ says error C2022: '2730': too big for character
.
The same problem happens with octal e.g. "\5555"
.
As far as I can see, perhaps the least awkward solution is to stop and restart the string literal, e.g. "\xaa" "a"
.
Is there a more compact/idiomatic solution?