c/c++中如何正常输出多字节字符控制台应用程序?
printf("%s\n", multibytestring);
默认情况下,多字节字符将在控制台中显示为 ???
,我该如何修复它?
printf("%s\n", multibytestring);
By default the multi-byte characters will show up like ???
in console, how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜是Windows,你指的是多字节字符而不是宽字符。
确保已定义
_MBCS
。尝试调用setlocale
然后调用_setmbcp
:之后它应该可以正常工作。
I'm guessing Windows, and that you mean multi-byte characters and not wide characters.
Make sure that
_MBCS
is defined. Try callingsetlocale
and then_setmbcp
:After that it should hopefully work fine.
您需要“%ls”来表示宽字符串。这就是你所追求的吗?
这是更完整的答案:
如果您询问 UTF8 字符显示不正确,它可能是特定于平台的(取决于控制台的代码页/处理)。您应该能够将 UTF8 输出为 ASCII,但是显示器必须能够正确处理 UTF8,并且您可能需要设置适当的代码页或其他环境设置(对此不太确定,我认为这是特定于应用程序/平台的)。
You want "%ls" for wide-character strings. Is that what you're after?
Here's a more complete answer:
If you're asking about UTF8 characters not appearing right, it's probably platform-specific (dependent on the codepage/handling for the console). You should be able to output UTF8 as ASCII, but the display will have to be able to handle UTF8 correctly, and you may need to set the appropriate codepage or other environment setting (not too sure about this, I think it's app/platform specific).
试试这个:
Try this: