char16_t印刷

时间:2011-04-10 12:39:04

标签: printing c++11 cout wchar-t char16-t

最近我将Windows应用程序移植到Linux时出现问题,因为这些平台之间存在wchar_t大小差异。我尝试使用编译器开关,但打印这些字符时出现问题(我认为GCC wcout认为所有wchar_t都是32位)。

所以,我的问题是:(w)cout char16_t有一个很好的方法吗?我问,因为它不起作用,我被迫把它投到wchar_t

cout << (wchar_t) c;

这似乎不是一个大问题,但它让我烦恼。

1 个答案:

答案 0 :(得分:2)

尝试一下:

#include <locale>
#include <codecvt>
#include <string>
#include <iostream>

int main()
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > myconv;
    std::wstring ws(L"Your UTF-16 text");
    std::string bs = myconv.to_bytes(ws);
    std::cout << bs << '\n';
}