C++:宽字符输出不正确?
我的代码基本上是这样的:
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,但它输出完全相同的十六进制代码点。诡异的!
My code is basically this:
wstring japan = L"日本";
wstring message = L"Welcome! Japan is ";
message += japan;
wprintf(message.c_str());
I'm wishing to use wide strings but I do not know how they're outputted, so I used wprintf. When I run something such as:
./widestr | hexdump
The hexidecimal codepoints create this:
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 ? ?
Why are they all jumped in order? I mean if the wprintf is wrong I still don't get why it'd output in such a specific jumbled order!
edit: endianness or something? they seem to rotate each two characters. huh.
EDIT 2: I tried using wcout, but it outputs the exact same hexidecimal codepoints. Weird!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
按预期定义语言环境 Works(即将宽字符串转换为窄 UTF-8 并打印它)。
当您将全局区域设置定义为“”时,您设置了系统区域设置(如果它是 UTF-8,则会
打印为 UTF-8 - 即 wstring 将被转换)
编辑:忘记我所说的关于sync_with_stdio的内容 - 这是不正确的,它们默认是同步的。不需要。
You need to define locale
Works as expected (i.e. convert wide string to narrow UTF-8 and print it).
When you define global locale to "" - you set system locale (and if it is UTF-8 it would
be printed out as UTF-8 - i.e. wstring will be converted)
Edit: forget what I said about sync_with_stdio -- this is not correct, they are synchronized by default. Not needed.