我的代码基本上是这样:

wstring japan = L"日本";
wstring message = L"Welcome! Japan is ";

message += japan;

wprintf(message.c_str());

我希望使用宽字符串,但我不知道它们是如何输出的,所以我使用了wprintf。当我运行诸如:

./widestr | hexdump

六边形编码点创建了以下方式:

65 57 63 6c 6d 6f 21 65 4a 20 70 61 6e 61 69 20 20 73 3f 3f
e  W  c  l  m  o  !  e  J     p  a  n  a  i        s  ?  ?

他们为什么都会井井有条?我的意思是,如果wprintf错了,我仍然不明白为什么它会以这种特定的混乱顺序输出!

编辑:末日之类的?他们似乎每两个字符旋转。嗯。

编辑2:我尝试使用WCOUT,但是它输出的六边形编码点完全相同。奇怪的!

有帮助吗?

解决方案

您需要定义语言环境

    #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;
    }

按预期工作(即将宽字符串转换为窄UTF-8并打印)。

当您将全局语言环境定义为“” - 您设置了系统语言环境(如果是UTF -8,则将其打印为UTF -8 - IE WSTRING将转换)

编辑: 忘了我对Sync_with_stdio的说法 - 这是不正确的,默认情况下它们是同步的。不需要。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top