• 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);
            }
        }
  • 相关阅读:
    JavaScript 数据类型判断
    使用渐进增强的方式美化复选框样式
    使用 Bootstrap 和 HTML5 Boilerplate 开始一个项目
    CSS基础知识之文本属性二三事
    精简CSS代码
    CSS选择器特殊性与重要性
    面试官:能解释一下javascript中的this吗
    VueRouter爬坑第四篇-命名路由、编程式导航
    Vuex实践(下)-mapState和mapGetters
    Vuex实践(中)-多module中的state、mutations、actions和getters
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9734269.html
Copyright © 2020-2023  润新知