C# 中英文与Unicode之间的相互装换

        /// <summary>
        /// unicode转中文(符合js规则的)
        /// </summary>
        /// <returns></returns>
        public 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;
        }


        /// 中英文转unicode
        /// </summary>
        /// <returns></returns>
        public string unicode_0(string str)
        {
            string outStr = "";
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    String ss = ((int)str[i]).ToString("x");
                    if (ss.Length != 4)
                    {
                        for (int jj = 0; jj <= 4 - ss.Length; jj++)
                        {
                            ss = "0" + ss;
                        }
                       
                    }
                    outStr += "\\u" + ss; 
                }
            }
            return outStr;
        }


sim900

using System.Text.RegularExpressions;


/// <summary>
        /// unicode转中文(符合js规则的)
        /// </summary>
        /// <returns></returns>
        public string unicode_js_1(string str)
        {
            String ss = "";
            //List<String> list = new List<string>();
            int num = str.Length / 4;
            for (int i = 0; i < num; i++ ) 
            {
                ss = ss + "\\u" + str.Substring(i * 4, 4);
            }

            str = ss;

            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+"//"+ss;
        }


        /// 中英文转unicode
        /// </summary>
        /// <returns></returns>
        public string unicode_0(string str)
        {
            string outStr = "";
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    String ss = ((int)str[i]).ToString("x");
                    if (ss.Length != 4)
                    {
                        for (int jj = 0; jj <= 4 - ss.Length; jj++)
                        {
                            ss = "0" + ss;
                        }
                       
                    }
                    outStr += "\\u" + ss; 
                }
            }
            outStr = outStr.Replace("\\u", "");
            return outStr;
        }

 /*
            String str = "";

            if (textBox1.Text == null) 
            {
                return;
               
            }

            str = textBox1.Text.ToString();

            byte[] data = {0x1A};

            char c = (char)data[0];

            int i = data[0];

            MessageBox.Show("" + c, i+"");
            */

            //SerialPort com = new SerialPort();
            //com.BaudRate = 115200;
            //com.PortName = "COM1";
            //com.DataBits = 8;
            //com.Open();//打开串口

            //com.Write(data);
            //com.Write(data,0,1);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值