0

我正在使用使用 UTF16 LE 解码文本的程序(如下例所示),因此我需要将编码恢复为字符才能使用它。

或者,如果我可以从 UTF16LE 转换为 UTF16BE,然后从 UTF16BE 转换为字符,则另一种解决方案将适用于我,这不是问题。

06F006E006A00750065003B00200043006F006D006D006100

或者

450631062D0628062706200043064A06410620002D062706440643061F0643064406200034064A0620002C064A062F061F06280627064A062C

4

1 回答 1

0

我通过将十六进制代码拆分为数组中的 4 个字符来解决该问题,然后我将代码交换为 UTF16BE 而不是 UTF16LE,之后,我将代码点转换为字符。

......

        string PointCode, words;
        int length = 4;
        int strLength = value.Length;
        int strCount = (strLength + length - 1) / length;
        string[] result = new string[strCount];
        char[] charcters = new char[strCount];

        for (int i = 0; i < strCount; ++i)
        {

            result[i] = value.Substring(i * length, Math.Min(length, strLength));
            strLength -= length;


            //Swap the bytes from UTF16 LE to UTF16 BE

            if (result[i].Length == 4)
            {
                string PointCode1 = result[i].Substring(0, 2);
                string PointCode2 = result[i].Substring(2, 2);

                PointCode = PointCode2 + PointCode1;

                charcters[i] = (char)Int16.Parse(PointCode, System.Globalization.NumberStyles.HexNumber);

            }
            else
            {
                PointCode = "00" + result[i];

                charcters[i] = (char)Int16.Parse(PointCode, System.Globalization.NumberStyles.HexNumber);

            }


        }
        words = (string.Join("", charcters));
        return words;
于 2019-09-04T09:46:00.603 回答