C# 中英文与Unicode之间的相互装换
2016-04-23 16:01
591 查看
/// <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);
相关文章推荐
- 【C#】System.IO.Stream 中flush的用法
- C#正则表达式
- C# 中的委托和事件
- C# DES (ECB模式) 加密解密 --单倍长
- z铭 WPF bitmap转bitmapimage 使用 CreateBitmapSourceFromHBitmap内存泄漏
- C# 如何获取出错的错误所在行数信息[原创]_C#教程_脚本之家
- C# 中堆与栈的浅记
- 新闻发布系统——静态方法使用问题(Static)
- C# Hello World WinForm
- 第四章:流程控制
- C# Hello World
- C#二叉树简易实例
- C# ASCII码排序
- C#中Directory.GetFiles() 函数的使用
- 进程(应用程序)之一
- C#OOP之二 变量和表达式
- C#OOP之二 变量和表达式
- C#OOP之二 变量和表达式
- C#_会员管理系统:开发三(修改密码)
- 你确实应该学习并使用的 10 个 C# 特性