1

在 Windows 10 及更早版本中,我已经能够将本地代码页 1250 中的字符串或使用CP_ACP以下代码成功传输到 UTF-8。但在 Windows 11 中,这不再适用CP_ACP(而 1250 仍然有效)。似乎默认代码页现在是 65001,无法通过这种方式转换为 UTF-8。结果简直是假的。

原因可能是,我的例子中的字符串“Öf”在65001中没有正确编码。现在我有一个大项目,用户输入字符串,各种第三方扮演角色,似乎都在1250中传递字符串,或非欧洲用户的当前代码页。

这是为什么?怎么办?

#include <Windows.h>

#include <cstdio>

int main()
{
    printf("UTF Conversation Test\n");

    char line[1000];
    WCHAR uline[1000];
    char uline1[1000];

    line[0] = 214;
    line[1] = 104;
    line[2] = 0;

    char *s1 = line;
    while (*s1 != 0)
    {
        printf("%10x %d\n", (int)*s1, (int)*s1);
        s1++;
    }
    printf("\n");

    MultiByteToWideChar(1250, 0, line, -1, uline, 1000);
    // MultiByteToWideChar(CP_ACP, 0, line, -1, uline, 1000);

    WCHAR* s2 = uline;

    while (*s2 != 0)
    {
        printf("%10x %d\n", (int)*s2, (int)*s2);
        s2++;
    }
    printf("\n");

    WideCharToMultiByte(CP_UTF8, 0, uline, -1, uline1, 1000, 0, 0);

    char *s3 = uline1;

    while (*s3 != 0)
    {
        printf("%10x %d\n", (int)*s3, (int)*s3);
        s3++;
    }
}
4

1 回答 1

2

事实证明,Windows 11 默认情况下会在系统范围内激活对 UTF-8 的 Beta 支持。这意味着任何在内部不以 Unicode 存储字符串的程序都必须转换为 UTF-8 并返回以使用 Windows 服务,例如字符的屏幕输出。更糟糕的是,他们的一些对话可能会停止正确显示本地字符。一种解决方案是在时间和地区的管理设置中禁用此 Beta 支持。

于 2021-12-07T15:47:56.017 回答