编码和解码笔记

命名空间

 

using System.Text;

 

将字符序列转换成字节序列的过程叫做编码
将字节反序列转换成字符序列的过程叫做解码
在C#中,字符默认都是Unicode码,即一个英文字符占两个字节,一个汉字也占两个字节
UTF是通用转换格式,一般用1-4个字节编码一个Unicode码。目前流行的UTF-8、UTF-16、UTF-32 

 

    //获取所有编码的名称和描述
        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();
        }

 

 

下面是一个例子

 

/********************************************************************* 
 * 创建人:Devilhand
 * 创建时间:2010-9-8
 * 说明: 编码和解码
 ********************************************************************/
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();
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值