Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;语言处理;在Unicode代码点转换中?_C#_.net_Unicode_Char - Fatal编程技术网

C# &引用;语言处理;在Unicode代码点转换中?

C# &引用;语言处理;在Unicode代码点转换中?,c#,.net,unicode,char,C#,.net,Unicode,Char,适用于以下国家的MSDN文件: 基本多语言平面(BMP)之外的有效代码点始终生成有效的代理项对但是,根据Unicode标准,BMP中的有效代码点可能不会产生有效的结果,因为转换过程中没有使用语言处理。因此,请使用System.Text::UTF32Encoding类将批量UTF-32数据转换为批量UTF-16数据 上面提到的“语言处理”是什么?对于BMP中的字符,Char.ConvertFromUtf32(i)[0]调用是否会产生与(Char)i不同的结果 for (int i = 0; i &

适用于以下国家的MSDN文件:

基本多语言平面(BMP)之外的有效代码点始终生成有效的代理项对但是,根据Unicode标准,BMP中的有效代码点可能不会产生有效的结果,因为转换过程中没有使用语言处理。因此,请使用System.Text::UTF32Encoding类将批量UTF-32数据转换为批量UTF-16数据

上面提到的“语言处理”是什么?对于BMP中的字符,
Char.ConvertFromUtf32(i)[0]
调用是否会产生与
(Char)i
不同的结果

for (int i = 0; i < 65535; i++)
{
    char ch1 = (char)i;

    if (i < 0x0d800 || i > 0xdfff)
    {
        string str1 = char.ConvertFromUtf32(i);

        if (str1.Length != 1)
        {
            Console.WriteLine("\\u+{0:x4}: char.ConvertFromUtf32(i).Length = {1}", i, str1.Length);
        }

        char ch2 = str1[0];

        if (ch1 != ch2)
        {
            Console.WriteLine("\\u+{0:x4}: (char)i = 0x{1:x4}, char.ConvertFromUtf32(i)[0] = 0x{2:x4}", i, (int)ch1, (int)ch2);
        }
    }

    byte[] bytes = BitConverter.GetBytes(i);
    string str2 = Encoding.UTF32.GetString(bytes);

    if (str2.Length != 1)
    {
        Console.WriteLine("\\u+{0:x4}: Encoding.UTF32.GetString(bytes).Length = {1}", i, str2.Length);
    }

    char ch3 = str2[0];

    if (ch1 != ch3)
    {
        Console.WriteLine("\\u+{0:x4}: (char)i = 0x{1:x4}, Encoding.UTF32.GetString(bytes)[0] = 0x{2:x4}", i, (int)ch1, (int)ch3);
    }
}

(我省略了与本讨论无关的各种代码行)

感谢您编写代码进行检查!该范围是为代理字符保留的,代理字符作为UTF-16之外的代码点无效,因此应使用异常/回退字符。如果这是唯一的区别,我假设MSDN文档是错误的,可能是指一些不应作为代码点转换一部分的Unicode规范化。@Douglas我甚至检查了
UTF32Encoding
的参考源,没有“特殊处理”遗憾的是,MSDN删除了该设施,以便在其文档中添加注释。像这样的错误应该被指出。
if (iChar >= 0x10000)
{
    *(chars++) = GetHighSurrogate(iChar);
    iChar = GetLowSurrogate(iChar);
}

// Add the rest of the surrogate or our normal character
*(chars++) = (char)iChar;