Asked
Active
Viewed 84 times
0
-
2Should just work, unless you're on Windows. If you are on Windows, [Unicode support](https://stackoverflow.com/a/63454192/4641116) for the C runtime. – Eljay Mar 30 '23 at 13:50
-
Encoding settings apply to files not to terminal. – metablaster Mar 30 '23 at 13:50
-
Yes I'm on windows – Jilowa Mar 30 '23 at 14:01
-
This might be a code page issue since you're on Windows. Try running `chcp 65001` to change the console's codepage to utf8. If you want to change the default, see https://superuser.com/q/269818/1749748. Does that answer your question? – starball Mar 30 '23 at 22:44
3 Answers
0
auto utf16_to_utf8 = [](const std::wstring &value) -> std::string {
int len = WideCharToMultiByte(CP_UTF8, 0, value.c_str(), -1, NULL, 0, 0, 0);
if (len)
{
std::string utf8 = std::string(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, value.c_str(), -1, &utf8[0], len, 0, 0);
return utf8;
}
return std::string();
};
auto utf8_to_utf16 = [](const std::string &value) -> std::wstring {
int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0, 0, 0);
if (len)
{
std::wstring utf16 = std::wstring(len, '\0');
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, &utf16[0], len, 0, 0);
return utf16;
}
return std::string();
};
Then do: std::wcout<<utf8_to_utf16(u8"Привет\n");
Windows terminals have been notoriously bad with displaying UTF-8, until recently in Windows 10 or 11. So you'd be better off outputting UTF-16.

Brandon
- 22,723
- 11
- 93
- 186
0
The solution was to tick "Use Unicode UTF-8 for worldwide language support" in the System Region Settings.

user16217248
- 3,119
- 19
- 19
- 37

Jilowa
- 1
- 2