在 Visual Studio 中使用 UTF-16 代替代码页进行 I/O
我使用代码页在 Visual Studio 2019 上进行此操作:
#include <windows.h>
#include <iostream>
int main()
{
UINT oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(932); //932 = Japanese.
//1200 for little-, 1201 big-, endian UTF-16
DWORD used;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),L"私の犬\n", 4,&used, 0);
std::cout << "Hit enter to end."; std::cin.get();
SetConsoleOutputCP(oldcp);
return 0;
}
但我从 Microsoft 看到我 不应使用代码页,除非与遗留代码交互 - 请改用 UTF-16。我可以找到 UTF-16(小端或大端)的代码页,但使用它们不起作用并且它仍在使用代码页。
那么我可以使用什么来完成我的程序的功能,但又是最新的呢?
I have this working on Visual Studio 2019 using code pages:
#include <windows.h>
#include <iostream>
int main()
{
UINT oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(932); //932 = Japanese.
//1200 for little-, 1201 big-, endian UTF-16
DWORD used;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),L"私の犬\n", 4,&used, 0);
std::cout << "Hit enter to end."; std::cin.get();
SetConsoleOutputCP(oldcp);
return 0;
}
But I am seeing from Microsoft that I should not be using code pages except to interface with legacy code -- use UTF-16 instead. I can find code pages for UTF-16 (little endian or big endian), but using them doesn't work and it's still using code pages.
So what can I use that accomplishes what my program does, but is up-to-date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Windows 中将 stdin 和 stdout 设置为宽模式,并将
wcout
和wcin
与宽字符串一起使用。您需要切换到控制台字体来支持字符,并使用 IME 来键入它们,这可以通过安装适当的语言支持来完成。您可以通过设置代码页自动获得该开关,但即使在“错误”的代码页中,字符也能正确输出。如果您选择支持字符的字体,它将起作用。输出(终端使用代码页 437 但使用 NSimSun 字体):
Set stdin and stdout to wide mode in Windows and use
wcout
andwcin
with wide strings. You'll need to switch to a console font to support the characters and and IME to type them as well, which can be accomplished by installing the appropriate language support. You're getting that switch automatically by setting a code page, but the characters output correctly even in the "wrong" code page. If you select a font that supports the characters it will work.Output (terminal using code page 437 but NSimSun font):
从技术上讲,每个字符编码都是一个代码页。要使用 UTF-16,您仍然必须指定 UTF-16“代码页”。但您还需要首先
_setmode
但是是吗?是最新的吗? 不!!! 打印 Unicode 最合理的方法是使用 UTF-8 代码页,这将使您的应用程序跨平台并且更易于维护。有关详细信息,请参阅en_US.UTF-8 语言环境的 Windows 等效项是什么?。基本上只
-A code> Win32 API 而不是
-W
如果您直接调用这些 API( MS 推荐的可移植性,因为其他人几十年来都在使用 UTF-8)/execution-charset:utf-8
和/或编译时的/utf-8
标志另请参阅是否可以将 Windows 应用程序的“区域设置”设置为 UTF -8?
Technically every character encoding is a code page. To use UTF-16 you still have to specify the UTF-16 "code page". But you also need to
_setmode
firstBut is it up-to-date? No!!! The most reasonable way to print Unicode is to use the UTF-8 code page which will make your app cross-platform and is easier to maintain. See What is the Windows equivalent for en_US.UTF-8 locale? for details on this. Basically just
-A
Win32 APIs instead of-W
ones if you're calling those directly (recommended by MS for portability, as everyone else was using UTF-8 for decades)/execution-charset:utf-8
and/or/utf-8
flags while compilingSee also Is it possible to set "locale" of a Windows application to UTF-8?