c# UTF-16转UTF-8 互转

2018-10-03 17:58:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VS发布web应用程序报:无法识别的特性“xmlns:xdt”。请注意特性

下一篇:c# 发邮件