I have a std::wstring like that:
std::wstring mystr = L"abc\U0000FFFF\U0000000A\U00000061\U00000010de";
I want to print it out to the console where it should look like that:
abc\U0000FFFF\U0000000A\U00000061\U00000010de
When I try this :
for (int i = 0; i < 9; i++)
{
char16_t* ctest = &c[i];
std::cout << *ctest<< " ";
}
the result is
61 62 63 ffff a 61 10 64 65
How can I get the my desired output?
Just inspect each character as you output it. If it's in the printable ASCII range, output it as a char; otherwise output it as a hex sequence.
std::wstring mystr = L"abc\U0000FFFF\U0000000A\U00000061\U00000010de";
for (int i = 0; i < mystr.size(); ++i)
{
if (mystr[i] >= 0x20 && mystr[i] < 0xff)
cout << (char)mystr[i];
else
cout << "\\U" << std::setfill('0') << std::right << std::setw(8) << std::hex << mystr[i];
}
The output differs from your desired output because you have the character "a" in there twice, once as a single character and once as a hex escape sequence. Once the string is compiled, there's no way to tell the difference between the two.