/*********************************************************************
* 创建人:Devilhand
* 创建时间:2010-9-8
* 说明: 编码和解码
********************************************************************/
将字符序列转换成字节序列的过程叫做编码
将字节反序列转换成字符序列的过程叫做解码
在C#中,字符默认都是Unicode码,即一个英文字符占两个字节,一个汉字也占两个字节
UTF是通用转换格式,一般用1-4个字节编码一个Unicode码。目前流行的UTF-8、UTF-16、UTF-32
//命名空间
using System.Text;
//获取所有编码的名称和描述
private void BindCodeType()
{
EncodingInfo[] enCodingInfo = Encoding.GetEncodings();
foreach (EncodingInfo ei in enCodingInfo)
{
Encoding en = ei.GetEncoding();
comboBox1.Items.Add(string.Format("{0}[{1}]", en.HeaderName , en.EncodingName));
}
comboBox1.SelectedIndex = comboBox1.FindString("gb2312");
}
//获取指定的编码的描述
private void GetEncode()
{
string edName = Encoding.GetEncoding("gb2312").EncodingName;
MessageBox.Show(edName);
}
//不同编码之间的转换
private void ConvertCode()
{
string str = "猜猜我是谁";
Byte[] unicodeBytes = Encoding.Unicode.GetBytes(str);
Byte[] utf8Bytes=Encoding.Convert (Encoding.Unicode ,Encoding.UTF8 ,unicodeBytes);
string result = Encoding.UTF8.GetString(utf8Bytes);
MessageBox.Show(result);
}
//当网络传输时,如果数据量较大,需要将其分开传输。如果直接使用Encoding类的GetBytes()方法,可能会出现一个数据快的末尾是一个高代理项,而与其对应的低代理项在下一个数据块中。此时,使用Encoder类可以很好地解决这个问题。
//Encoder类的构造函数是Protected的,所以不能直接实例化Encoder对象,而要通过Encoding类的GetEncoder()方法类实例化对象
//创建Encoder的实例(如UTF-8编码)
Encoder ed = Encoding.UTF8.GetEncoder();
//Encoder对象的GetBytes()方法(编码)
//Encoder对象的GetByteCount()方法获取字符数组序列化后得到的字节数组的长度
private byte[] EncoderGetBytes(string str)
{
char[] chars = str.ToCharArray();
Encoder ed = Encoding.UTF8.GetEncoder();
Byte[] bytes = new Byte[ed.GetByteCount(chars, 0, chars.Length, true)];
ed.GetBytes(chars, 0, chars.Length, bytes, 0, true);
return bytes;
}
//Decoder对象GetChars()方法
//Decoder对象的GetCharCount()方法获取字节数组反序列化后得到的字符数组的长度
private string DecoderGetChars(Byte[] bytes)
{
Decoder dd = Encoding.UTF8.GetDecoder();
char[] chars = new char[dd.GetCharCount(bytes, 0, bytes.Length, true)];
dd.GetChars(bytes, 0, bytes.Length, chars, 0, true);
StringBuilder sb = new StringBuilder(100);
foreach (char c in chars )
{
sb.Append(c);
}
return sb.ToString();
}
下面是一个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 编码和解码
{
public partial class EncodeAndDecode : Form
{
private string codeType = null;//编码类型
Byte[] bytes;//字节数组
public EncodeAndDecode()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEncode();
MyDecode();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "猜猜我是谁?";
textBox2.ReadOnly = textBox3.ReadOnly = true;
BindCodeType();
}
//获取所有编码的名称和描述
private void BindCodeType()
{
EncodingInfo[] enCodingInfo = Encoding.GetEncodings();
foreach (EncodingInfo ei in enCodingInfo)
{
Encoding en = ei.GetEncoding();
comboBox1.Items.Add(string.Format("{0}[{1}]", en.HeaderName, en.EncodingName));
}
comboBox1.SelectedIndex = comboBox1.FindString("gb2312");
}
//编码
private void MyEncode()
{
codeType = comboBox1.SelectedItem.ToString();
codeType = codeType.Substring(0, codeType.IndexOf('['));
Encoder ed = Encoding.GetEncoding(codeType).GetEncoder();
char[] chars = textBox1.Text.Trim().ToCharArray();
bytes = new Byte[ed.GetByteCount(chars, 0, chars.Length, true)];
ed.GetBytes(chars, 0, chars.Length, bytes, 0, true);
textBox2.Text = Convert.ToBase64String(bytes);
}
//解码
private void MyDecode()
{
Decoder dd = Encoding.GetEncoding(codeType).GetDecoder();
//Decoder dd = Encoding.Default.GetDecoder();
char[] chars = new char[dd.GetCharCount(bytes, 0, bytes.Length, true)];
dd.GetChars(bytes, 0, bytes.Length, chars, 0, true);
StringBuilder result = new StringBuilder(1000);
foreach (char c in chars)
{
result.Append(c);
}
textBox3.Text = result.ToString();
}
}
}