gpt4 book ai didi

c++ - 为什么设置了中文代码页的 Windows 控制台可以显示 UTF-16 编码的字符?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:22 25 4
gpt4 key购买 nike

根据 MSDN :

"For the Microsoft C/C++ compiler, the source and execution character sets are both ASCII."

C++03

2.1 翻译阶段

"..Any source file character not in the basic source character set (2.2) is replaced by the universal-character-name that designates that character. (An implementation may use any internal encoding, so long as an actual extended character encountered in the source file, and the same extended character expressed in the source file as a universal-character-name (i.e. using the \uXXXX notation), are handled equivalently.)"

2.13.2 字符字面量

"A universal-character-name is translated to the encoding, in the execution character set, of the character named. If there is no such encoding, the universal-character-name is translated to an implementation-defined encoding."

为了测试MSVC++使用的是哪个执行字符集,我写了如下代码:

wchar_t *str = L"中";
unsigned char *p = reinterpret_cast<unsigned char*>(str);
for (int i = 0; i < sizeof(L"中"); ++i)
{
printf ("%x ", *(p + i));
}

输出显示2d 4e 0 00x4e2dUTF-16 encoding这个汉字。所以我得出结论:UTF-16被MSVC用作执行字符集(我的版本:2012 4.5.50709)

之后,我尝试将此字符打印到 Windows 控制台。由于控制台使用的默认语言环境是“C”,因此我将语言环境设置为表示简体中文字符的代码页936。

// use the execution environment locale setting, which is 936
wchar_t *str = L"中";
char* locale = setlocale(LC_ALL, "");
wprintf (L"%ls\n", str);

哪些输出:


我很好奇的是,一个以 UTF-16 编码的字符如何被区域设置(解码器)设置为非 UTF-16(MS 代码页 936)的 Windows 控制台解码?怎么会这样?

最佳答案

how can a character encoded in UTF-16 be decoded by a Windows console whose locale(decoder) is set to non-UTF-16

有两种方法可以将文本写入控制台。字节方式使用 Win32 API WriteConsoleA,为您提供来自使用控制台代码页(“ANSI”)解释的字节的字符。 Unicode 方式 WriteConsoleW 接收 UTF-16LE 字符串并将字符直接写入控制台,而不必担心它使用的是什么代码页。

当输出是交互式控制台时,stdio 函数 printf 使用 WriteConsoleAwprintf 函数,至少从 VS 2005 开始,调用 WriteConsoleW

关于c++ - 为什么设置了中文代码页的 Windows 控制台可以显示 UTF-16 编码的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16350099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com