#副標=.NET Framework與Unicode #大標=C#與Unicode的應用 #-----box程式----- protected Encoding(int codepage); -----end----- -----box程式----- public static Encoding GetEncoding( int codepage //代表Code Page的整數值 ); -----end----- -----box程式----- public static Encoding GetEncoding( string name //Code Page的名稱 ); -----end----- -----box程式----- public virtual int GetByteCount( string s //原始字串 ); -----end----- -----box程式----- public virtual int GetByteCount( char[] chars //原始字元陣列,一般而言是由原始字串得來 ); -----end----- -----box程式----- public abstract int GetByteCount( char[] chars, //原始字元陣列 int index, //字元陣列要編碼的起始位置 int count //字元陣列要編碼的個數 ); -----end----- -----box程式----- public virtual byte[] GetBytes( string s //原始字串 ); -----end----- -----box程式----- public virtual byte[] GetBytes( char[] chars //原始字元陣列,一般而言是由原始字串得來 ); -----end----- -----box程式----- public virtual byte[] GetBytes( char[] chars, //原始字元陣列 int index, //字元陣列要編碼的起始位置 int count //字元陣列要編碼的個數 ); -----end----- -----box程式----- public virtual int GetCharCount( byte[] bytes //要進行解碼的位元陣列 ); -----end----- -----box程式----- public abstract int GetCharCount( byte[] bytes, //要進行解碼的位元陣列 int index, //位元陣列開始進行解碼的起始位置 int count //要進行解碼的位元成員總數 ); -----end----- -----box程式----- public virtual char[] GetChars( byte[] bytes //要進行解碼的位元陣列 ); -----end----- -----box程式----- public virtual char[] GetChars( byte[] bytes, //要進行解碼的位元陣列 int index, //要進行解碼的位元陣列起始位置 int count //位元陣列要進行解碼的位元總數 ); -----end----- -----box程式----- public abstract int GetChars( byte[] bytes, //要進行解碼的位元陣列 int byteIndex, //要進行解碼的位元陣列起始位置 int byteCount, //位元陣列要進行解碼的位元總數 char[] chars, //解碼後所產生的目的字元陣列 int charIndex //目的字元陣列加入解碼字元的起始位置 ); -----end----- -----box程式----- public static byte[] Convert( Encoding srcEncoding, //來源編碼格式,以Encoding類別物件實體表示 Encoding dstEncoding, //目的編碼格式,以Encoding類別物件實體表示 byte[] bytes //來源編碼格式的位元陣列值 ); -----end----- -----box程式----- public static byte[] Convert( Encoding srcEncoding, //來源編碼格式,以Encoding類別物件實體表示 Encoding dstEncoding, //目的編碼格式,以Encoding類別物件實體表示 byte[] bytes, //來源編碼格式的位元陣列值 int index, //來源編碼格式的位元陣列裡要進行解碼的起始位置 int count //來源編碼格式的位元陣列裡要進行解碼的位元個數 ); -----end----- -----box程式----- 1.using System.Text; -----end----- -----box程式----- 1.private void ToCodePageDecode(int codepage) 2.{ 3. Encoding targetEncoding; 4. int byte_counter = 0; 5. byte []bytes; 6. char []chars; 7. char []target_chars; 8. string s_Decode = ""; 9. 10. targetEncoding = Encoding.GetEncoding(codepage); 11. chars = this.textBox1.Text.ToCharArray(); 12. target_chars = new char[chars.Length]; 13. byte_counter = targetEncoding.GetByteCount(chars, 0,chars.Length); 14. bytes = new byte[byte_counter]; 15. bytes = targetEncoding.GetBytes(this.textBox1.Text); 16. this.textBox2.Text += "\r\nCode Page " + 17. targetEncoding.CodePage + " " + 18. targetEncoding.EncodingName + "\r\n"; 19. for (int idx = 0; idx < bytes.Length; idx++) 20. { 21. this.textBox2.Text += " " + bytes[idx].ToString(); 22. } 23. this.textBox2.Text += "\r\n"; 24. targetEncoding.GetChars(bytes, 0, bytes.Length, target_chars, 0); 25. s_Decode = new String(target_chars); 26. this.textBox2.Text += "Decode -> " + s_Decode; 27.}         -----end----- -----box程式----- 1.private void button1_Click(object sender, System.EventArgs e) 2.{ 3. this.textBox2.Text = ""; 4. this.ToCodePageDecode(437); 5. this.ToCodePageDecode(850); 6. this.ToCodePageDecode(852); 7. this.ToCodePageDecode(932); 8. this.ToCodePageDecode(949); 9. this.ToCodePageDecode(950); 10. this.ToCodePageDecode(1252); 11.}         -----end----- -----box程式----- 1.private void ToCodePageDecode(int codepage) 2.{ 3. Encoding targetEncoding; 4. Encoder targetEncoder; 5. Decoder targetDecoder; 6. int byte_counter = 0; 7. byte []bytes; 8. char []chars; 9. string s_Decode = ""; 10. 11. 12. targetEncoding = Encoding.GetEncoding(codepage); 13. targetEncoder = targetEncoding.GetEncoder(); 14. targetDecoder = targetEncoding.GetDecoder(); 15. chars = this.textBox1.Text.ToCharArray(); 16. byte_counter = targetEncoder.GetByteCount(chars, 0, chars.Length, true); 17. bytes = new byte[byte_counter]; 18. targetEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, true); 19. this.textBox2.Text += "\r\nCode Page " + 20. targetEncoding.CodePage + " " + 21. targetEncoding.EncodingName + "\r\n"; 22. for (int idx = 0; idx < bytes.Length; idx++) 23. { 24. this.textBox2.Text += " " + bytes[idx].ToString(); 25. } 26. this.textBox2.Text += "\r\n"; 27. targetDecoder.GetChars(bytes, 0,bytes.Length, chars, 0); 28. s_Decode = new String(chars); 29. this.textBox2.Text += "Decode -> " + s_Decode; 30.} -----end----- -----box程式----- 1.private void ToCodaPage(int source_page, int target_page) 2.{ 3. Encoding sourceEncoding, targetEncoding; 4. char []chars = this.textBox1.Text.ToCharArray(); 5. byte []bytes; 6. byte []target_bytes; 7. int byte_counter = 0; 8. string s_Decode = ""; 9. 10. sourceEncoding = Encoding.GetEncoding(source_page); 11. targetEncoding = Encoding.GetEncoding(target_page); 12. byte_counter = sourceEncoding.GetByteCount(chars, 0,chars.Length); 13. bytes = new byte[byte_counter]; 14. bytes = sourceEncoding.GetBytes(this.textBox1.Text); 15. byte_counter = targetEncoding.GetByteCount(chars, 0,chars.Length); 16. target_bytes = new byte[byte_counter]; 17. target_bytes = Encoding.Convert(sourceEncoding, targetEncoding, bytes); 18. s_Decode = targetEncoding.GetString(target_bytes); 19. this.textBox2.Text += "\r\nSource Code Page " + 20. sourceEncoding.CodePage + " " + 21. sourceEncoding.EncodingName + "\r\n"; 22. this.textBox2.Text += "\r\nTarget Code Page " + 23. targetEncoding.CodePage + " " + 24. targetEncoding.EncodingName + "\r\n"; 25. this.textBox2.Text += "\r\nSource Byte -> "; 26. for (int idx = 0; idx < bytes.Length; idx++) 27. { 28. this.textBox2.Text += " " + bytes[idx].ToString(); 29. } 30. this.textBox2.Text += "\r\n"; 31. this.textBox2.Text += "\r\nTarget Byte -> "; 32. for (int idx = 0; idx < target_bytes.Length; idx++) 33. { 34. this.textBox2.Text += " " + target_bytes[idx].ToString(); 35. } 36. this.textBox2.Text += "\r\n"; 37. this.textBox2.Text += "\r\nSource String -> "; 38. this.textBox2.Text += this.textBox1.Text; 39. this.textBox2.Text += "\r\n"; 40. this.textBox2.Text += "\r\nTarget String -> "; 41. this.textBox2.Text += s_Decode; 42. this.textBox2.Text += "\r\n"; 43.}        -----end-----