.net六种汉字代码转换(gbk_utf8_u…
2016-02-24 11:35
483 查看
.net六种汉字代码转换(gbk_utf8_unicode)
private void button1_Click(object sender, EventArgs e)
{
//汉字转为Unicode编码:
string hz
= textBox1.Text.ToString();
byte[]
b=Encoding.Unicode.GetBytes(hz);
string o =
"";
foreach(var x in b){
o +=
string.Format("{0:X2}",x) + " ";
}
textBox2.Text = o;
}
private void button2_Click(object sender, EventArgs e)
{
//Unicode编码转为汉字:
string cd
= textBox2.Text.ToString();
string cd2
= cd.Replace(" ", "");
cd2 = cd2.Replace("\r", "");
cd2 = cd2.Replace("\n", "");
cd2 = cd2.Replace("\r\n", "");
cd2 = cd2.Replace("\t", "");
if
(cd2.Length % 4 != 0)
{
MessageBox.Show("Unicode编码为双字节,请删多或补少!确保是二的倍数。");
}
else
{
int len = cd2.Length /
2;
byte[] b = new
byte[len];
for (int i = 0; i
< cd2.Length;i+=2 )
{
string bi = cd2.Substring(i, 2);
b[i/2] =(byte) Convert.ToInt32(bi, 16);
}
string
o=Encoding.Unicode.GetString(b);
textBox1.Text = o;
}
}
private void button5_Click(object sender,
EventArgs e)
{
//汉字转成GBK十六进制码:
string hz
= textBox3.Text.ToString();
byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(hz);
string s1
= ""; string s1d = "";
foreach(byte b in gbk){
//s1 += Convert.ToString(b,
16)+" ";
s1 += string.Format("{0:X2}",
b) + " ";
s1d += b + " ";
toolTip1.SetToolTip(textBox4,
s1d);
}
textBox4.Text = s1;
toolTip1.SetToolTip(textBox4, s1d);
//汉字转成Unicode十六进制码:
byte[] uc
= Encoding.Unicode.GetBytes(hz);
string s2
= ""; string s2d = "";
foreach
(byte b in uc)
{
//s2 += Convert.ToString(b,
16) + " ";
s2 += string.Format("{0:X2}",
b) + " ";
s2d += b + " ";
toolTip1.SetToolTip(textBox5,
s2d);
}
textBox5.Text = s2;
toolTip1.SetToolTip(textBox5, s2d);
//汉字转成UTF-8十六进制码:
byte[]
utf8 = Encoding.UTF8.GetBytes(hz);
string s3
= ""; string s3d = "";
foreach
(byte b in utf8)
{
//s3 += Convert.ToString(b,
16) + " ";
s3 += string.Format("{0:X2}",
b) + " ";
s3d += b + " ";
toolTip1.SetToolTip(textBox6,
s3d);
}
textBox6.Text = s3;
toolTip1.SetToolTip(textBox6, s3d);
}
private void button6_Click(object sender,
EventArgs e)
{ //GBK十六进制码转成汉字:
string cd
= textBox4.Text.ToString();
string[]
b4 = cd.Split(' ');
byte[]
bs=new byte[2];
bs[0] =
(byte)Convert.ToByte(b4[0], 16);
bs[1] =
(byte)Convert.ToByte(b4[1], 16);
textBox3.Text =Encoding.GetEncoding("GBK").GetString(bs);
}
private void button7_Click(object sender,
EventArgs e)
{ //Unicode十六进制码转成汉字:
string cd
= textBox5.Text.ToString();
string[]
b5 = cd.Split(' ');
byte[] bs
= new byte[2];
bs[0] =
(byte)Convert.ToByte(b5[0], 16);
bs[1] =
(byte)Convert.ToByte(b5[1], 16);
textBox3.Text =
Encoding.GetEncoding("Unicode").GetString(bs);
}
private void button8_Click(object sender,
EventArgs e)
{ //UTF-8十六进制码转成汉字:
string cd
= textBox6.Text.ToString();
string[]
b6 = cd.Split(' ');
byte[] bs
= new byte[3];
bs[0] =
(byte)Convert.ToByte(b6[0], 16);
bs[1] =
(byte)Convert.ToByte(b6[1], 16);
bs[2] =
(byte)Convert.ToByte(b6[2], 16);
textBox3.Text = Encoding.GetEncoding("UTF-8").GetString(bs);
}
private void button1_Click(object sender, EventArgs e)
{
//汉字转为Unicode编码:
string hz
= textBox1.Text.ToString();
byte[]
b=Encoding.Unicode.GetBytes(hz);
string o =
"";
foreach(var x in b){
o +=
string.Format("{0:X2}",x) + " ";
}
textBox2.Text = o;
}
private void button2_Click(object sender, EventArgs e)
{
//Unicode编码转为汉字:
string cd
= textBox2.Text.ToString();
string cd2
= cd.Replace(" ", "");
cd2 = cd2.Replace("\r", "");
cd2 = cd2.Replace("\n", "");
cd2 = cd2.Replace("\r\n", "");
cd2 = cd2.Replace("\t", "");
if
(cd2.Length % 4 != 0)
{
MessageBox.Show("Unicode编码为双字节,请删多或补少!确保是二的倍数。");
}
else
{
int len = cd2.Length /
2;
byte[] b = new
byte[len];
for (int i = 0; i
< cd2.Length;i+=2 )
{
string bi = cd2.Substring(i, 2);
b[i/2] =(byte) Convert.ToInt32(bi, 16);
}
string
o=Encoding.Unicode.GetString(b);
textBox1.Text = o;
}
}
private void button5_Click(object sender,
EventArgs e)
{
//汉字转成GBK十六进制码:
string hz
= textBox3.Text.ToString();
byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(hz);
string s1
= ""; string s1d = "";
foreach(byte b in gbk){
//s1 += Convert.ToString(b,
16)+" ";
s1 += string.Format("{0:X2}",
b) + " ";
s1d += b + " ";
toolTip1.SetToolTip(textBox4,
s1d);
}
textBox4.Text = s1;
toolTip1.SetToolTip(textBox4, s1d);
//汉字转成Unicode十六进制码:
byte[] uc
= Encoding.Unicode.GetBytes(hz);
string s2
= ""; string s2d = "";
foreach
(byte b in uc)
{
//s2 += Convert.ToString(b,
16) + " ";
s2 += string.Format("{0:X2}",
b) + " ";
s2d += b + " ";
toolTip1.SetToolTip(textBox5,
s2d);
}
textBox5.Text = s2;
toolTip1.SetToolTip(textBox5, s2d);
//汉字转成UTF-8十六进制码:
byte[]
utf8 = Encoding.UTF8.GetBytes(hz);
string s3
= ""; string s3d = "";
foreach
(byte b in utf8)
{
//s3 += Convert.ToString(b,
16) + " ";
s3 += string.Format("{0:X2}",
b) + " ";
s3d += b + " ";
toolTip1.SetToolTip(textBox6,
s3d);
}
textBox6.Text = s3;
toolTip1.SetToolTip(textBox6, s3d);
}
private void button6_Click(object sender,
EventArgs e)
{ //GBK十六进制码转成汉字:
string cd
= textBox4.Text.ToString();
string[]
b4 = cd.Split(' ');
byte[]
bs=new byte[2];
bs[0] =
(byte)Convert.ToByte(b4[0], 16);
bs[1] =
(byte)Convert.ToByte(b4[1], 16);
textBox3.Text =Encoding.GetEncoding("GBK").GetString(bs);
}
private void button7_Click(object sender,
EventArgs e)
{ //Unicode十六进制码转成汉字:
string cd
= textBox5.Text.ToString();
string[]
b5 = cd.Split(' ');
byte[] bs
= new byte[2];
bs[0] =
(byte)Convert.ToByte(b5[0], 16);
bs[1] =
(byte)Convert.ToByte(b5[1], 16);
textBox3.Text =
Encoding.GetEncoding("Unicode").GetString(bs);
}
private void button8_Click(object sender,
EventArgs e)
{ //UTF-8十六进制码转成汉字:
string cd
= textBox6.Text.ToString();
string[]
b6 = cd.Split(' ');
byte[] bs
= new byte[3];
bs[0] =
(byte)Convert.ToByte(b6[0], 16);
bs[1] =
(byte)Convert.ToByte(b6[1], 16);
bs[2] =
(byte)Convert.ToByte(b6[2], 16);
textBox3.Text = Encoding.GetEncoding("UTF-8").GetString(bs);
}
相关文章推荐
- 用PHP伪造来路信息
- 性能分析之-- JAVA Thread Dump
- 一些小的编程技巧
- vb.net webbrowser控件如何获取鼠…
- lua学习:lua中“类”的实现
- golang模板语法简明教程
- vb.net DateTimePicker如何显示某…
- 如何使用vb.net多线程
- vb.net多线程同步调用实例
- EditPlus 技巧大全:在文件中查找...
- How to deal with Robot Framework result from XML output
- vb document.execCommand()语法格…
- Java_观察者模式(Observable和Observer) -转
- C++ map的基本操作和使用
- 万网whois查询接口代码
- vb.net批量查询已删除域名
- VB.net怎么最小化到系统托盘
- vb.net 判断是否按下某个按键
- 淘宝开放平台php-sdk测试
- PHP正则表达式入门必读