简体   繁体   English

如何在C / C ++控制台应用程序中正常输出utf8编码的字符?

[英]How to output utf8 encoded characters normally in c/c++ console application?

Here's what I'm getting now by wprintf : 这是wprintf现在提供的信息:

1胩?鳧?1敬爄汯?瑳瑡獵猆慴畴??

Is utf8 just not supported by windows? Windows不支持utf8吗?

No, Windows doesn't support printing UTF-8 to the console. 不,Windows不支持将UTF-8打印到控制台。

When Windows says "Unicode", it means UTF-16. Windows表示“ Unicode”时,表示UTF-16。 You need to use MultiByteToWideChar to convert from UTF-8 to UTF-16. 您需要使用MultiByteToWideChar从UTF-8转换为UTF-16。 Something like this: 像这样:

char* text = "My UTF-8 text\n";
int len = MultiByteToWideChar(CP_UTF8, 0, text, -1, 0, 0);
wchar_t *unicode_text = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, text, -1, unicode_text, len);
wprintf(L"%s", unicode_text);

wprintf supposed to receive a UTF-16 encoded string. wprintf应该接收UTF-16编码的字符串。 Use the following for conversion: 使用以下内容进行转换:

Use MultiByteToWideChar with CP_UTF8 codepage to do the conversion. 使用具有CP_UTF8代码页的MultiByteToWideChar进行转换。 (and don't do blind casting from char* into wchar_t* ). (并且不要从char*wchar_t*盲目转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM