中国conversion依Multibytetowidechar
我正在尝试在MessageBoxw中显示中文文本。但是我无法正确将其从UTF-8转换为WCHAR_T。同时,正确显示原始的WCHAR_T中文。 我玩过不同的多teToWideChar标志,但结果相同。错误转换的原因是什么?
I'm trying to display a Chinese text in the MessageBoxW. But I can't correctly convert it from UTF-8 to wchar_t. At the same time, the original wchar_t Chinese is displayed correctly.
I played with different MultiByteToWideChar flags but with the same result. What the reason of the incorrect conversion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
char text [] =“文本”
仅在UTF-8中编码源文件时才是UTF-8。由于您的标题字符串正确显示您的编码是Windows上的默认中文旧版编码,而text
字符串字符串包含该编码中的字节,而不是UTF-8,因此MultibyTetoWideChar
失败。您可以看到该函数如果设置标志以检查无效字符,则该函数将返回零,如果不是真正的UTF-8:Microsoft编译器具有指定源和执行字符集的选项,以及
/ a
/ UTF-8
选项(建议):要修复的多个选项。 #2和#3假定Microsoft编译器。其他编译器可能会有所不同。
char text [] = u8“文本”;
由于您现有的默认编码支持中文。源字符将在该编码中解释,然后用该符号重新编码UTF-8。如果将源发送给具有不同OS默认编码的人,则该源对他们不起作用。/utf-8
一样。文本
将包含UTF-8字节。标题将正确显示。/utf-8
开关进行编译,以告知编译器将源解码为UTF-8而不是默认编码。#4的示例将正确编译,无论OS默认编码如何:
char text[] = "文本"
is only UTF-8 if the source file is encoded in UTF-8. Since your title string displays correctly your encoding is the default Chinese legacy encoding on Windows, and thetext
string contains bytes in that encoding, and not UTF-8, soMultiByteToWideChar
fails. You can see that the function returns zero if you set the flag to check for invalid characters, which happens if it isn't really UTF-8:The Microsoft compiler has options to specify source and execution character set, and a
/utf-8
option (recommended):Multiple options to fix. #2 and #3 assume the Microsoft compiler. Other compilers may vary.
char text[] = u8"文本";
since your existing default encoding supports Chinese. The source characters will be interpreted in that encoding and then re-encoded in UTF-8 with this notation. If the source is sent to someone with different OS default encoding, it will not work for them./utf-8
was specified.text
will contain UTF-8 bytes. Title will display correctly./utf-8
switch to inform the compiler to decode the source as UTF-8 instead of the default encoding.Example of #4 that will compile correctly no matter the OS default encoding: