程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp: string Encoding,csharpencoding

csharp: string Encoding,csharpencoding

編輯:C#入門知識

csharp: string Encoding,csharpencoding


  /// <summary>
        /// 中文轉unicode
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string unicode_0(string str)
        {
            string outStr = "";
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    outStr += "/u" + ((int)str[i]).ToString("x");
                }
            }
            return outStr;
        }
        /// <summary>
        /// 漢字轉為Unicode編碼
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string bgktounicode(string str)
        {
            string outstr = "";
            //漢字轉為Unicode編碼:
            string hz = str;
            byte[] b = Encoding.Unicode.GetBytes(hz);
            string o = "";
            foreach (var x in b)
            {
                o += string.Format("{0:X2}", x) + " ";
            }
            outstr = o;
            return outstr;
        }

        /// <summary>
        ///  unicode轉中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string unicode_1(string str)
        {
            string outStr = "";
            if (!string.IsNullOrEmpty(str))
            {
                string[] strlist = str.Replace("/", "").Split('u');
                try
                {
                    for (int i = 1; i < strlist.Length; i++)
                    {
                        //將unicode字符轉為10進制整數,然後轉為char中文字符  
                        outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                    }
                }
                catch (FormatException ex)
                {
                    outStr = ex.Message;
                }
            }
            return outStr;
        }
        /// <summary>
        /// unicode轉中文(符合js規則的)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string unicode_js_1(string str)
        {
            string outStr = "";
            Regex reg = new Regex(@"(?i)\\u([0-9a-f]{4})");
            outStr = reg.Replace(str, delegate(Match m1)
            {
                return ((char)Convert.ToInt32(m1.Groups[1].Value, 16)).ToString();
            });
            return outStr;
        }
        /// <summary>
        /// 中文轉unicode(符合js規則的)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string unicode_js_0(string str)
        {
            string outStr = "";
            string a = "";
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    if (Regex.IsMatch(str[i].ToString(), @"[\u4e00-\u9fa5]")) { outStr += "\\u" + ((int)str[i]).ToString("x"); }
                    else { outStr += str[i]; }
                }
            }
            return outStr;
        }

        /// <summary>
        /// 骞垮憡涓戦椈
        /// </summary>
        /// <param name="utf8String"></param>
        /// <returns></returns>
        public static string unicodeTogbk(string utf8String)
        {
            string defaultString = "";
            Encoding utf8 = Encoding.UTF8;
            Encoding defaultCode = Encoding.Default;
            // Convert the string into a byte[].
            byte[] utf8Bytes = Encoding.Default.GetBytes(utf8String);
            // Perform the conversion from one encoding to the other.
            byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes);
            // Convert the new byte[] into a char[] and then into a string.
            // This is a slightly different approach to converting to illustrate
            // the use of GetCharCount/GetChars.
            char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes, 0, defaultBytes.Length)];
            defaultCode.GetChars(defaultBytes, 0, defaultBytes.Length, defaultChars, 0);
            defaultString = new string(defaultChars);
            return defaultString;
        }
        /// <summary>
        /// 骞垮憡涓戦椈
        /// </summary>
        /// <param name="utf8String"></param>
        /// <returns></returns>
        public static string unicodeTogbkb(string utf8String)
        {
            string strBuffer = "";
            byte[] buffer1 = Encoding.Default.GetBytes(utf8String);
            byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
            strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
            return strBuffer;
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved