zoukankan      html  css  js  c++  java
  • 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);
            }
        }
  • 相关阅读:
    理解SSL、HTTPS原理中的对称加密与非对称加密
    lib下的Jar包在项目打包的时候提示不能找不到
    springboot2.0 最大上传文件大小遇到的错误Failed to bind properties under 'spring.servlet.multipart.max-file-size'
    Executors创建线程池的几种方式以及使用
    JAVA深入研究——Method的Invoke方法(转)
    git merge 与 git rebase的区别
    git rebase
    git fetch , git pull
    nginx搭建(centos7)
    idea过期激活
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9734269.html
Copyright © 2011-2022 走看看