符号化と復号【回転】


/*********************************************************************
*作成者:Devilhand
*作成日時:2010-9-8
*説明:エンコードとデコード
********************************************************************/
文字シーケンスをバイトシーケンスに変換するプロセスを符号化と呼ぶ
バイトの逆シーケンスを文字シーケンスに変換するプロセスを復号と呼びます
C#では、文字のデフォルトはUnicodeコードです.つまり、1つの英語文字が2バイト、1つの漢字も2バイトです.
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();
        }

    }
}