C ++: are invalid characters returned?

My code is basically like this:

wstring japan = L"ζ—₯本"; wstring message = L"Welcome! Japan is "; message += japan; wprintf(message.c_str()); 

I want to use wide strings, but I don't know how they are output, so I used wprintf. When I run something like:

 ./widestr | hexdump 

Heterodical code points create this:

 65 57 63 6c 6d 6f 21 65 4a 20 70 61 6e 61 69 20 20 73 3f 3f e W clmo ! e J panais ? ? 

Why are they all bouncing around? I mean, if wprintf is wrong, I still don’t understand why it would issue in such a specific messy order!

edit: endianness or something else? they seem to rotate every two characters. Yes.

EDIT 2: I tried using wcout, but it outputs exactly the same hexadecimal code points. Weird!

+6
c ++ string unicode widechar
source share
1 answer

You need to define locale

  #include <stdio.h> #include <string> #include <locale> #include <iostream> using namespace std; int main() { std::locale::global(std::locale("")); wstring japan = L"ζ—₯本"; wstring message = L"Welcome! Japan is "; message += japan; wprintf(message.c_str()); wcout << message << endl; } 

It works as expected (i.e., converts a wide string to a narrow UTF-8 and prints it).

When you define the global locale to "" - you set the system language (and if it is UTF-8, it will print as UTF-8 - that is, wstring will be converted)

Edit: forget what I said about sync_with_stdio - this is not true, they are synchronized by default. Not required.

+11
source share

All Articles