下面的代码在 VS2019 中打印 pi 数字的字符,而不是字符`ã`。我错过了什么?
The code below prints the character for the pi number in VS2019, instead of the character `ã`. What am I missing?
下面的代码在 VS2019 中打印 pi 数字的字符,而不是字符 ã
。
#include<iostream>
const char* p = "\u00E3"; // Character ã LATIN SMALL LETTER A WITH TILDE
int main() {
std::cout << p << '\n'; // This should compile by any compiler supporting some ASCII compatible encoding.
// It does compile in clang and GCC, printing `ã` in both. In VS2019 it prints the symbol for
// the pi number. However if I debug the code I can see the character `ã` in
// memory in the address given by `p`. What am I missing?
}
参见 Coliru 中的 demo。
编辑
被认为是重复的问题没有像上面的代码那样使用通用字符名称。因此这应该在 VS2019 中正确编译和执行。
是的,U+00E3 是 ã
的代码点。那就是只是一个数字。该数字必须经过编码才能存储在任何地方(内存、文件等)。你有一个编码问题。您将字节 0xe3 写入终端,但其代码页为 cp437,其中 0xe3 被解码为 π
.
在 Windows 上,可以使用宽字符串并将终端模式设置为 UTF-16。
#include<iostream>
#include <io.h>
#include <fcntl.h>
const wchar_t* p = L"\u00E3"; // wide string
int main() {
_setmode(_fileno(stdout), _O_U16TEXT); // set console mode
std::wcout << p << '\n';
}
输出(使用 VS2019 和 运行 在 Windows 10 控制台 window 中编译):
ã
请注意,控制台使用的字体必须支持打印的字符,否则您将得到替换字符 U+FFFD �
(此字符的外观也会因字体而异)。
下面的代码在 VS2019 中打印 pi 数字的字符,而不是字符 ã
。
#include<iostream>
const char* p = "\u00E3"; // Character ã LATIN SMALL LETTER A WITH TILDE
int main() {
std::cout << p << '\n'; // This should compile by any compiler supporting some ASCII compatible encoding.
// It does compile in clang and GCC, printing `ã` in both. In VS2019 it prints the symbol for
// the pi number. However if I debug the code I can see the character `ã` in
// memory in the address given by `p`. What am I missing?
}
参见 Coliru 中的 demo。
编辑
被认为是重复的问题没有像上面的代码那样使用通用字符名称。因此这应该在 VS2019 中正确编译和执行。
是的,U+00E3 是 ã
的代码点。那就是只是一个数字。该数字必须经过编码才能存储在任何地方(内存、文件等)。你有一个编码问题。您将字节 0xe3 写入终端,但其代码页为 cp437,其中 0xe3 被解码为 π
.
在 Windows 上,可以使用宽字符串并将终端模式设置为 UTF-16。
#include<iostream>
#include <io.h>
#include <fcntl.h>
const wchar_t* p = L"\u00E3"; // wide string
int main() {
_setmode(_fileno(stdout), _O_U16TEXT); // set console mode
std::wcout << p << '\n';
}
输出(使用 VS2019 和 运行 在 Windows 10 控制台 window 中编译):
ã
请注意,控制台使用的字体必须支持打印的字符,否则您将得到替换字符 U+FFFD �
(此字符的外观也会因字体而异)。