当前位置:首页 > .NET

在不使用C# Encoding类情况下编码转换

大滑稽12年前 (2014-11-22).NET1504

        /// <summary>
        /// 中文转unicode
         /// </summary>
         /// <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>
         /// <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>
         /// <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>
         /// <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;
         }

扫描二维码推送至手机访问。

版权声明:本文由第★一★次发布,如需转载请注明出处。

本文链接:https://blog.wpers.net/post/23.html

标签:C#.NET
分享给朋友:

“在不使用C# Encoding类情况下编码转换” 的相关文章

C#遍历控件的方法

首先,要想遍历,就必须找到你想找的表单里面的所有控件,然后一个个的逐一比对,当找到了你需要的控件的时候,再做你需要的操作。1、foreach方法foreach (Control control in …

Linq读写XML

         private List<News> GetNews(string html)    &n…

修改注册表限制软件使用次数

private void Form1_Load(object sender, System.EventArgs e){RegistryKey RootKey,RegKey; //项名为:HKEY_CURRENT_USER\Software…

c#中分页显示数据

     //c#中分页显示数据    public partial class Form1 : Form    {  …

以ToolStrip为例绘制简便背景

//以ToolStrip为例绘制简便背景e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Point(…

C#程序防止重复运行

 Boolean mutexWasCreated;//声明一个Boolean值,用于下面的Out//true 为是否给予当前这个线程互斥的功能, true为是, false为否,也就是说是否不允许两个相同名称的线程存在//可以…