MSVC UTF8字符串编码使用了不正确的代码点
MSVC UTF8 string encoding uses incorrect code points
我正在尝试写字符"Ā"(https://www.fileformat.info/info/unicode/char/0100/index.htm)转换为C++11 UTF8字符串(使用u8
前缀(。
const char *const utf8 = u8"Ā";
const char *const utf8_2 = u8"u0100";
const char *const chars = "Ā";
const int utf8_len = strlen(utf8);
const int utf8_2_len = strlen(utf8_2);
const int chars_len = strlen(chars);
在MSVC(16.2.4(下运行此操作会导致:
utf8_len == 5
utf8_2_len = 2;
chars_len = 2;
其中:
utf8 == "Ä€"
utf8_2 == "Ä€"
chars == "Ä€"
源文件设置为UTF8(不带BOM表(。
尝试同样的Clang和GCC工作如预期:
https://godbolt.org/z/PNZFCa
有人知道为什么会发生这种行为吗?为什么前缀为u8
的Unicode字符被编码为5字节(当它应该是2时(?
Microsoft编译器对没有BOM的文件采用本地ANSI编码,在您的情况下可能是Windows-1252。如果您从命令行运行cl /?
,您将看到以下命令行开关:
...
/source-charset:<iana-name>|.nnnn set source character set
/execution-charset:<iana-name>|.nnnn set execution character set
/utf-8 set source and execution character set to UTF-8
...
如果不想与BOM一起保存,请使用/source-charset:UTF-8
或/utf-8
。
以UTF-8保存的测试代码,不带BOM:
#include <stdio.h>
#include <string.h>
int main()
{
const char *const utf8 = u8"Ā";
printf("%zun",strlen(utf8));
}
输出:
C:>cl /nologo test.cpp
test.cpp
C:>test
5
C:>cl /nologo /utf-8 test.cpp
test.cpp
C:>test
2
相关文章:
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 如何让我的代码显示文件名不正确或文件中数字的平均值?
- MSVC UTF8字符串编码使用了不正确的代码点
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- 对于代码中指定的边界之外的值,对角差算法的输入不正确
- 下面的代码是格式不正确的 NDR 还是格式正确的代码
- 为什么隐式类型转换在此代码中结果不正确
- Visual C 实际上会生成公然不正确的代码
- 我的假设是下面的代码不正确的NDR?但为什么
- 项目代码同时正确和不正确地注册'N'
- “在成员函数之外封闭类的定义中需要默认成员初始值设定项” - 我的代码格式不正确
- 上一个较大的数字代码中的输出不正确
- 代码阻止不正确的 c++ 输出
- OpenGL中的着色器代码不正确.我想念什么
- 我认为我的代码很完美,但输出不正确
- waitpid() 给出了不正确的退出代码
- 我的代码语法不正确的地方
- 代码::BLOCKS-为什么路径不正确
- Arduino Uno代码创建不正确的时间值
- 漂亮的打印 - 使用格式不正确的C++代码测试漂亮的打印机