欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# UTF-16转UTF-8 互转

程序员文章站 2022-05-12 12:37:50
/// /// UTF-16转UTF-8 /// /// /// public static string UTF16To8(string str) { string res; int i, len, c; res =... ......
  /// <summary>
        /// utf-16转utf-8
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string utf16to8(string str)
        {
            string res;
            int i, len, c;
            res = "";
            len = str.length;
            for (i = 0; i < len; i++)
            {
                c = convert.tobyte(str[i]);
                if ((c >= 0x0001) && (c <= 0x007f))
                {
                    res += str.charat(i);
                }
                else if (c > 0x07ff)
                {
                    res += convert.tochar(0xe0 | ((c >> 12) & 0x0f));
                    res += convert.tochar(0x80 | ((c >> 6) & 0x3f));
                    res += convert.tochar(0x80 | ((c >> 0) & 0x3f));
                }
                else
                {
                    res += convert.tochar(0xc0 | ((c >> 6) & 0x1f));
                    res += convert.tochar(0x80 | ((c >> 0) & 0x3f));
                }
            }
            return res;
        }

 

 /// <summary>
        /// utf-8转utf-16
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string utf8to16(string str)
        {
            string res;
            int i, len, c;
            int char2, char3;
            res = "";
            len = str.length;
            i = 0;
            while (i < len)
            {
                c = convert.tobyte(str[i++]);
                switch (c >> 4)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        // 0xxxxxxx
                        res += str.charat(i - 1);
                        break;
                    case 12:
                    case 13:
                        // 110x xxxx 10xx xxxx
                        char2 = convert.tobyte(str[i++]);
                        res += convert.tochar(((c & 0x1f) << 6) | (char2 & 0x3f));
                        break;
                    case 14:
                        // 1110 xxxx 10xx xxxx 10xx xxxx
                        char2 = convert.tobyte(str[i++]);
                        char3 = convert.tobyte(str[i++]);
                        res += convert.tochar(((c & 0x0f) << 12) |
                            ((char2 & 0x3f) << 6) |
                            ((char3 & 0x3f) << 0));
                        break;
                }
            }
            return res;
        }
public static class te
    {
        /// <summary>
        /// 返回指定位置字符
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="index">字符索引,长度超出时返回:' '</param>
        /// <returns></returns>
        public static char charat(this string str, int index)
        {
            if (index > str.length)
                return ' ';

            string res = str.substring(index, 1);
            return convert.tochar(res);
        }
    }