c# UTF-16转UTF-8 互转

2021-07-08 19:05

阅读:557

标签:+=   ring   returns   dex   switch   span   utf-16   lse   else   

  /// 
        /// UTF-16转UTF-8
        /// 
        /// 
        /// 
        public static string UTF16To8(string str)
        {
            string res;
            int i, len, c;
            res = "";
            len = str.Length;
            for (i = 0; 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;
        }

 

 /// 
        /// UTF-8转UTF-16
        /// 
        /// 
        /// 
        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
    {
        /// 
        /// 返回指定位置字符
        /// 
        /// 原字符串
        /// 字符索引,长度超出时返回:‘ ‘
        /// 
        public static char CharAt(this string str, int index)
        {
            if (index > str.Length)
                return  ;

            string res = str.Substring(index, 1);
            return Convert.ToChar(res);
        }
    }

 

c# UTF-16转UTF-8 互转

标签:+=   ring   returns   dex   switch   span   utf-16   lse   else   

原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9734269.html

上一篇:c# Base64解密加密

下一篇:c# Json操作


评论


亲,登录后才可以留言!