c# UTF-16转UTF-8 互转

  /// <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);
        }
    }

 

posted @ 2018-10-01 10:56  紅人  阅读(4037)  评论(0编辑  收藏  举报