当前位置: 移动技术网 > IT编程>开发语言>.net > c# UTF-16转UTF-8 互转

c# UTF-16转UTF-8 互转

2018年10月02日  | 移动技术网IT编程  | 我要评论

绑恋 父子,琉璃光中医诊所,exb是什么文件

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网