c#汉字与编码之间的转换(输出十六进制)
2015-03-18 21:48
435 查看
/******************************************************************/ /*********************** ****************************/ /*********************** 汉字转换工具 ****************************/ /*********************** ****************************/ /******************************************************************/ /**************************** 字符串转编码函数 **********************************/ private byte[] StringToBytes(string TheString) { Encoding encoding = Encoding.GetEncoding("UTF-8"); Encoding encoding2 = Encoding.GetEncoding("gb2312"); byte[] bytes = encoding.GetBytes(TheString); return Encoding.Convert(encoding, encoding2, bytes); } /**************************** 编码转字符串函数 **********************************/ private string BytesToString(byte[] Bytes) { Encoding encoding = Encoding.GetEncoding("gb2312"); Encoding encoding2 = Encoding.GetEncoding("UTF-8"); byte[] bytes = Encoding.Convert(encoding, encoding2, Bytes); return encoding2.GetString(bytes); } /**************************** 单击转换按钮事件 **********************************/ private void Changez_Click(object sender, EventArgs e) { if (this.CHcode.Checked)//判断什么类型的转换 { byte[] array = this.StringToBytes(this.intextz.Text); this.outtextz.Text = ""; byte[] array2 = array; for (int i = 0; i < array2.Length; i++) { byte b = array2[i]; string text = b.ToString("x").ToUpper(); TextBox expr_64 = this.outtextz; expr_64.Text = expr_64.Text + "0x" + ((text.Length == 1) ? ("0" + text) : text) + " "; } } else { if (!this.CHcode.Checked) { byte[] array3 = new byte[this.intextz.Text.Length / 2]; try { string text2 = this.intextz.Text; text2 = text2.Replace("0x", ""); text2 = text2.Replace(" ", string.Empty); for (int j = 0; j < text2.Length / 2; j++) { array3[j] = Convert.ToByte(text2.Substring(j * 2, 2), 16); } this.outtextz.Text = this.BytesToString(array3); } catch { MessageBox.Show("数据转换错误,请输入数字。", "错误"); } } } }
相关文章推荐
- C#_汉字与GBK,Unicode,UTF-8编码之间的转换(by daode1212)
- C#_汉字与GBK,Unicode,UTF-8编码之间的转换
- C#_汉字与GBK,Unicode,UTF-8编码之间的转换(by daode1212)
- C# 字节数组和十六进制字符串之间转换的另类写法
- .Net(c#)汉字和Unicode编码互相转换
- Unicode 码和汉字,十进制之间互转!二进制、十六进制,十进制的转换
- C#各种进制之间数据转换二进制、八进制、十进制、十六进制
- C#汉字Unicode编码相互转换
- C# - 汉字与unicode之间的转换
- C# 十六进制字符串与数值类型之间转换
- java 中转换html 实体编码为汉字---unicode编码(\u)、十进制(&#)、十六进制(&#x)
- c#中int32与byte[]之间的互换,以及base64编码的转换
- 字符串转换成十六进制(包含汉字)c#
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- 汉字百分比编码之间转换
- C# 十六进制字符串与数值类型之间转换
- C#汉字Unicode编码相互转换
- C语言unsigned char与char之间的转换及英文字符ASCII码和汉字内码输出
- C#汉字Unicode编码相互转换
- C# 十六进制字符串与数值类型之间转换