赞
踩
C#怎么将GBK编码转换成UTF-8编码?
private void ChangeEncode(Encoding fromCoding,Encoding toCoding,string fileName)
{
var text = System.IO.File.ReadAllText(fileName,fromCoding);
System.IO.File.WriteAllText(fileName, text, toCoding);
}
大概这样
C#中的string类型一律是UCS2(或者UTF16?这里的区别我不太了解……)的
只有byte[]类型可以代表GBK或者UTF8编码的字符串
C# GBK编码的字符串与UTF_8编码的字符串如何相互转换_ …… C#中的string类型一律是UCS2(或者UTF16?这里的区别我不太了解……)的 只有byte[]类型可以代表GBK或者UTF8编码的字符串 System.Text.Encoding.UTF8.GetBytes <- string转UTF8的byte[] System.Text.Encoding.UTF8.GetString <- UTF8的...
C#如何将gbk编码的字符串改成UTF_8的编码? …… c#中的string类型一律是ucs2(或者utf16?这里的区别我不太了解……)的只有byte[]类型可以代表gbk或者utf8编码的字符串 system.text.encoding.utf8.getbytes 转string -> 转utf8的byte[]反过来也一样
c# 编码转换 字符串utf_8 如何转换成GBK格式 …… C# codestring s=""; byte[] buffer= Encoding.UTF8.GetBytes(S); Text =Encoding.GetEncoding( "GBK ").GetString(buffer);
C#如何对字符串进行UTF_ …… Encoding.Default.GetString采用的Default Encoding.UTF8.GetBytes采用的是utf-8编码.这样当然是乱码.尤其出现中文时候.对这类数据处理当然要用统一的编码来处理.例 子:1 string msg= Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(strcode)); 例子:2 string strcode="我是小明"; byte[] buffer=Encoding.UTF8.GetBytes(strcode); string msg= Encoding.UTF8.GetString(buffer,0,buffer.Length);
C#如何将字符串转换成UTF8编码_ …… using System.Text; byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes("www.9iBee.com"); string str = ""; foreach (byte b in buffer) str += string.Format("%{0:X}", b);
c# 字符串进行gbk转码怎么实现_ …… system.text.encoding.default.getsbytes()可以实现.事实上,在.net中只实现gb2312,并未实现gbk 编码.两者是一个包含关系.system.text.encoding.defualt其实就是操作系统的默认编码,所以最好的办法是 system.text.encoding.getencoding...
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);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。