更新:2007 年 11 月
命名空間:
System.Text 組件:
mscorlib (在 mscorlib.dll 中)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Encoding _
Implements ICloneable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Encoding : ICloneable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Encoding abstract : ICloneable
/** @attribute SerializableAttribute */
/** @attribute ComVisibleAttribute(true) */
public abstract class Encoding implements ICloneable
public abstract class Encoding implements ICloneable
編碼是將一組 Unicode 字元轉換成位元組序列的處理程序。相反地,解碼則是將已編碼的位元組序列轉換成一組 Unicode 字元的處理程序。如需 Unicode Transformation Format (UTF) 和 Encoding 所支援之其他編碼的詳細資訊,請參閱瞭解編碼。請參閱使用 Unicode 編碼方式。
請注意,Encoding 主要是處理 Unicode 字元,而不是任意的二進位資料,例如位元組陣列。如果您的應用程式必須將任意二進位資料編碼成文字,應該使用諸如 uuencode 的通訊協定;該通訊協定是由 ConvertToBase64CharArray()()() 之類的方法所實作。
.NET Framework 提供下列 Encoding 類別實作來支援目前的 Unicode 編碼方式和其他編碼方式:
ASCIIEncoding 可編碼 Unicode 字元成單一 7 位元 ASCII 字元。這種編碼方式只支援 U+0000 和 U+007F 之間的字元值。字碼頁 20127。也可透過 ASCII 屬性取得。
UTF7Encoding 會使用 UTF-7 編碼方式為 Unicode 字元編碼。這種編碼方式支援所有的 Unicode 字元值。字碼頁 65000。也可透過 UTF7 屬性取得。
UTF8Encoding 會使用 UTF-8 編碼方式為 Unicode 字元編碼。這種編碼方式支援所有的 Unicode 字元值。字碼頁 65001。也可透過 UTF8 屬性取得。
UnicodeEncoding 會使用 UTF-16 編碼方式為 Unicode 字元編碼。同時支援位元組由小到大 (字碼頁 1200) 和位元組由大到小 (字碼頁 1201) 的位元組順序。也可透過 Unicode 屬性和 BigEndianUnicode 屬性取得。
UTF32Encoding 會使用 UTF-32 編碼方式為 Unicode 字元編碼。同時支援位元組由小到大 (字碼頁 12000) 和位元組由大到小 (字碼頁 12001) 的位元組順序。也可透過 UTF32 屬性取得。
Encoding 類別主要用於在不同的編碼與 Unicode 之間轉換。通常,一個衍生的 Unicode 類別就是適合應用程式的正確選擇。
您的應用程式會使用 GetEncoding 方法取得其他編碼方式。它們應該會使用 GetEncodings 方法取得所有編碼方式的清單。
下表列出支援的編碼方式和其關聯的字碼頁。最後一欄中的星號指示 .NET Framework 原本就支援此字碼頁 (不論基礎平台為何)。
字碼頁
|
Name
|
顯示名稱
| |
|---|
37
|
IBM037
|
IBM EBCDIC (美國-加拿大)
| |
437
|
IBM437
|
OEM 美國
| |
500
|
IBM500
|
IBM EBCDIC (國際)
| |
708
|
ASMO-708
|
阿拉伯文 (ASMO 708)
| |
720
|
DOS-720
|
阿拉伯文 (DOS)
| |
737
|
ibm737
|
希臘文 (DOS)
| |
775
|
ibm775
|
波羅的海文 (DOS)
| |
850
|
ibm850
|
西歐語系 (DOS)
| |
852
|
ibm852
|
中歐語系 (DOS)
| |
855
|
IBM855
|
OEM 斯拉夫文
| |
857
|
ibm857
|
土耳其文 (DOS)
| |
858
|
IBM00858
|
OEM 多語系拉丁文 I
| |
860
|
IBM860
|
葡萄牙文 (DOS)
| |
861
|
ibm861
|
冰島文 (DOS)
| |
862
|
DOS-862
|
希伯來文 (DOS)
| |
863
|
IBM863
|
加拿大法文 (DOS)
| |
864
|
IBM864
|
阿拉伯文 (864)
| |
865
|
IBM865
|
北歐字母 (DOS)
| |
866
|
cp866
|
斯拉夫文 (DOS)
| |
869
|
ibm869
|
希臘文,現代 (DOS)
| |
870
|
IBM870
|
IBM EBCDIC (多語系拉丁文 2)
| |
874
|
windows-874
|
泰文 (Windows)
| |
875
|
cp875
|
IBM EBCDIC (希臘文現代)
| |
932
|
shift_jis
|
日文 (Shift-JIS)
| |
936
|
gb2312
|
簡體中文 (GB2312)
|
*
|
949
|
ks_c_5601-1987
|
韓文
| |
950
|
big5
|
繁體中文 (Big5)
| |
1026
|
IBM1026
|
IBM EBCDIC (土耳其拉丁文 5)
| |
1047
|
IBM01047
|
IBM 拉丁文 1
| |
1140
|
IBM01140
|
IBM EBCDIC (美國-加拿大-歐洲)
| |
1141
|
IBM01141
|
IBM EBCDIC (德國-歐洲)
| |
1142
|
IBM01142
|
IBM EBCDIC (丹麥-挪威-歐洲)
| |
1143
|
IBM01143
|
IBM EBCDIC (芬蘭-瑞典-歐洲)
| |
1144
|
IBM01144
|
IBM EBCDIC (義大利-歐洲)
| |
1145
|
IBM01145
|
IBM EBCDIC (西班牙-歐洲)
| |
1146
|
IBM01146
|
IBM EBCDIC (英國-歐洲)
| |
1147
|
IBM01147
|
IBM EBCDIC (法國-歐洲)
| |
1148
|
IBM01148
|
IBM EBCDIC (國際-歐洲)
| |
1149
|
IBM01149
|
IBM EBCDIC (冰島文-歐洲)
| |
1200
|
utf-16
|
Unicode
|
*
|
1201
|
unicodeFFFE
|
Unicode (位元組由大到小)
|
*
|
1250
|
windows-1250
|
中歐語系 (Windows)
| |
1251
|
windows-1251
|
斯拉夫文 (Windows)
| |
1252
|
Windows-1252
|
西歐語系 (Windows)
|
*
|
1253
|
windows-1253
|
希臘文 (Windows)
| |
1254
|
windows-1254
|
土耳其文 (Windows)
| |
1255
|
windows-1255
|
希伯來文 (Windows)
| |
1256
|
windows-1256
|
阿拉伯文 (Windows)
| |
1257
|
windows-1257
|
波羅的海文 (Windows)
| |
1258
|
windows-1258
|
越南文 (Windows)
| |
1361
|
韓文
|
韓文 (Johab)
| |
10000
|
Macintosh
|
西歐語系 (Mac)
| |
10001
|
x-mac-japanese
|
日文 (Mac)
| |
10002
|
x-mac-chinesetrad
|
繁體中文 (Mac)
| |
10003
|
x-mac-korean
|
韓文 (Mac)
|
*
|
10004
|
x-mac-arabic
|
阿拉伯文 (Mac)
| |
10005
|
x-mac-hebrew
|
希伯來文 (Mac)
| |
10006
|
x-mac-greek
|
希臘文 (Mac)
| |
10007
|
x-mac-cyrillic
|
斯拉夫文 (Mac)
| |
10008
|
x-mac-chinesesimp
|
簡體中文 (Mac)
|
*
|
10010
|
x-mac-romanian
|
羅馬尼亞文 (Mac)
| |
10017
|
x-mac-ukrainian
|
烏克蘭文 (Mac)
| |
10021
|
x-mac-thai
|
泰文 (Mac)
| |
10029
|
x-mac-ce
|
中歐語系 (Mac)
| |
10079
|
x-mac-icelandic
|
冰島文 (Mac)
| |
10081
|
x-mac-turkish
|
土耳其文 (Mac)
| |
10082
|
x-mac-croatian
|
克羅埃西亞文 (Mac)
| |
12000
|
utf-32
|
Unicode (UTF-32)
|
*
|
12001
|
utf-32BE
|
Unicode (UTF-32 位元組由大到小)
|
*
|
20000
|
x-Chinese-CNS
|
繁體中文 (CNS)
| |
20001
|
x-cp20001
|
TCA 台灣
| |
20002
|
x-Chinese-Eten
|
繁體中文 (倚天)
| |
20003
|
x-cp20003
|
IBM5550 台灣
| |
20004
|
x-cp20004
|
TeleText 台灣
| |
20005
|
x-cp20005
|
Wang 台灣
| |
20105
|
x-IA5
|
西歐語系 (IA5)
| |
20106
|
x-IA5-German
|
德文 (IA5)
| |
20107
|
x-IA5-Swedish
|
瑞典文 (IA5)
| |
20108
|
x-IA5-Norwegian
|
挪威文 (IA5)
| |
20127
|
us-ascii
|
US-ASCII
|
*
|
20261
|
x-cp20261
|
T.61
| |
20269
|
x-cp20269
|
ISO-6937
| |
20273
|
IBM273
|
IBM EBCDIC (德國)
| |
20277
|
IBM277
|
IBM EBCDIC (丹麥-挪威)
| |
20278
|
IBM278
|
IBM EBCDIC (芬蘭-瑞典)
| |
20280
|
IBM280
|
IBM EBCDIC (義大利)
| |
20284
|
IBM284
|
IBM EBCDIC (西班牙)
| |
20285
|
IBM285
|
IBM EBCDIC (英國)
| |
20290
|
IBM290
|
IBM EBCDIC (日文片假名)
| |
20297
|
IBM297
|
IBM EBCDIC (法國)
| |
20420
|
IBM420
|
IBM EBCDIC (阿拉伯文)
| |
20423
|
IBM423
|
IBM EBCDIC (希臘文)
| |
20424
|
IBM424
|
IBM EBCDIC (希伯來文)
| |
20833
|
x-EBCDIC-KoreanExtended
|
IBM EBCDIC (韓文擴充)
| |
20838
|
IBM-Thai
|
IBM EBCDIC (泰文)
| |
20866
|
koi8-r
|
斯拉夫文 (KOI8-R)
| |
20871
|
IBM871
|
IBM EBCDIC (冰島文)
| |
20880
|
IBM880
|
IBM EBCDIC (斯拉夫俄文)
| |
20905
|
IBM905
|
IBM EBCDIC (土耳其文)
| |
20924
|
IBM00924
|
IBM 拉丁文 1
| |
20932
|
EUC-JP
|
日文 (JIS 0208-1990 和 0212-1990)
| |
20936
|
x-cp20936
|
簡體中文 (GB2312-80)
|
*
|
20949
|
x-cp20949
|
韓文 Wansung
|
*
|
21025
|
cp1025
|
IBM EBCDIC (斯拉夫塞爾維亞文-保加利亞文)
| |
21866
|
koi8-u
|
斯拉夫文 (KOI8-U)
| |
28591
|
iso-8859-1
|
西歐語系 (ISO)
|
*
|
28592
|
iso-8859-2
|
中歐語系 (ISO)
| |
28593
|
iso-8859-3
|
拉丁文 3 (ISO)
| |
28594
|
iso-8859-4
|
波羅的海文 (ISO)
| |
28595
|
iso-8859-5
|
斯拉夫文 (ISO)
| |
28596
|
iso-8859-6
|
阿拉伯文 (ISO)
| |
28597
|
iso-8859-7
|
希臘文 (ISO)
| |
28598
|
iso-8859-8
|
希伯來文 (ISO-Visual)
|
*
|
28599
|
iso-8859-9
|
土耳其文 (ISO)
| |
28603
|
iso-8859-13
|
愛沙尼亞文 (ISO)
| |
28605
|
iso-8859-15
|
拉丁文 9 (ISO)
| |
29001
|
x-Europa
|
歐洲
| |
38598
|
iso-8859-8-i
|
希伯來文 (ISO-Logical)
|
*
|
50220
|
iso-2022-jp
|
日文 (JIS)
|
*
|
50221
|
csISO2022JP
|
日文 (JIS-允許 1 位元組片假名)
|
*
|
50222
|
iso-2022-jp
|
日文 (JIS-允許 1 位元組片假名 - SO/SI)
|
*
|
50225
|
iso-2022-kr
|
韓文 (ISO)
|
*
|
50227
|
x-cp50227
|
簡體中文 (ISO-2022)
|
*
|
51932
|
euc-jp
|
日文 (EUC)
|
*
|
51936
|
EUC-CN
|
簡體中文 (EUC)
|
*
|
51949
|
euc-kr
|
韓文 (EUC)
|
*
|
52936
|
hz-gb-2312
|
簡體中文 (HZ)
|
*
|
54936
|
GB18030
|
簡體中文 (GB18030)
|
*
|
57002
|
x-iscii-de
|
ISCII 梵文語系
|
*
|
57003
|
x-iscii-be
|
ISCII 孟加拉文
|
*
|
57004
|
x-iscii-ta
|
ISCII 坦米爾文
|
*
|
57005
|
x-iscii-te
|
ISCII 特拉古文
|
*
|
57006
|
x-iscii-as
|
ISCII 阿薩姆文
|
*
|
57007
|
x-iscii-or
|
ISCII 歐利亞文
|
*
|
57008
|
x-iscii-ka
|
ISCII 坎那達文
|
*
|
57009
|
x-iscii-ma
|
ISCII 馬來亞拉姆文
|
*
|
57010
|
x-iscii-gu
|
ISCII 古吉拉特文
|
*
|
57011
|
x-iscii-pa
|
ISCII 旁遮普語
|
*
|
65000
|
utf-7
|
Unicode (UTF-7)
|
*
|
65001
|
utf-8
|
Unicode (UTF-8)
|
*
|
GetByteCount()()() 方法判斷多少位元組會編碼一組 Unicode 字元,而 GetBytes()()() 方法則執行實際的編碼作業。GetBytes()()() 方法需要分開的轉換,而不像 GetBytes()()() 方法會在單一輸入資料流上處理多次轉換。
支援的 GetByteCount()()() 和 GetBytes()()() 有多種版本。下列是使用這些方法時的一些程式設計考量:
應用程式可能需要將許多輸入字元編碼為某個字碼頁,並使用多次呼叫來處理字元。在這種情況下,您的應用程式需要維護呼叫之間的狀態,並考量所使用之 Encoder 物件所保存的狀態。
如果應用程式會處理字串輸入,則建議使用字串版本的 GetBytes()()()。
Unicode 字元緩衝區版本的 GetBytes()()() 提供一些快速技巧,特別適用於使用 Encoder 物件多次呼叫或者是插入至現有的緩衝區。切記,這個版本的方法需要指標,因此有時並不安全。
如果您的應用程式必須轉換大量資料,則應該重複使用輸出緩衝區。在這種情況下,支援位元組陣列的 GetBytes()()() 版本是最佳選擇。
考慮使用 Encoder..::.Convert 方法來代替 GetByteCount()()()。轉換方法會盡可能轉換資料,如果輸出緩衝區太小,就會擲回例外狀況 (Exception)。如果是連續的資料流編碼,這個方法通常是最佳選擇。
GetCharCount()()() 方法會判斷有多少字元會造成位元組序列的解碼,而由 GetChars()()() 方法執行實際解碼。GetChars()()() 方法需要分開的轉換,而不像 GetChars()()() 方法會在單一輸入資料流上處理多次傳遞。
支援的 GetCharCount()()() 和 GetChar()()() 有多種版本。下列是使用這些方法時的一些程式設計考量:
應用程式可能需要從某個字碼頁解碼多個輸入位元組,並使用多次呼叫來處理位元組。在這種情況下,您的應用程式可能需要維護呼叫之間的狀態。
如果應用程式會處理字串輸出,則建議使用 GetString()()() 方法。這個方法必須檢查字元長度並配置緩衝區,因此速度稍慢,但會產生較合需要的 String 型別。
位元組版本的 GetChar()()() 提供一些快速技巧,特別適用於多次呼叫大型緩衝區。切記,這個版本的方法需要指標,因此有時並不安全。
如果您的應用程式必須轉換大量資料,則應該重複使用輸出緩衝區。在這種情況下,支援輸出字元緩衝區的 GetChar()()() 版本是最佳選擇。
考慮使用 Decoder..::.Convert 方法來代替 GetCharCount()()()。轉換方法會盡可能轉換資料,如果輸出緩衝區太小,就會擲回例外狀況。如果是連續的資料流解碼,這個方法通常是最佳選擇。
如果要轉換的資料只可以從循序區塊取得 (例如從資料流讀取的資料) 時,或資料量太大而必須分成較小區塊時,您的應用程式應該使用衍生類別 (Derived Class) 的 GetDecoder 方法或 GetEncoder 方法提供的 Decoder 或 Encoder。
UTF-16 和 UTF-32 編碼器可以使用位元組由大到小的位元組順序 (最大顯著性位元組在前) 或位元組由小到大的位元組順序 (最小顯著性位元組在前)。例如,拉丁大寫字母 A (U+0041) 會序列化如下 (使用十六進位):
UTF-16 位元組由大到小的位元組順序:00 41
UTF-16 位元組由小到大的位元組順序:41 00
UTF-32 位元組由大到小的位元組順序:00 00 00 41
UTF-32 位元組由小到大的位元組順序:41 00 00 00
使用原生的位元組順序來儲存 Unicode 字元,通常會更有效率。例如,在位元組由小到大的平台 (如 Intel 電腦) 上,最好使用位元組由小到大位元組順序。
GetPreamble 方法會擷取包含位元組順序標記 (BOM) 的位元組陣列。如果此位元組陣列有前置編碼的資料流,將有助於解碼器識別所用的編碼格式。
如需位元組順序和位元組順序標記的詳細資訊,請參閱以下網頁中的 Unicode 標準:Unicode Home Page。
請注意,編碼類別可讓錯誤:
建議您的應用程式在所有資料流錯誤上擲回例外狀況。應用程式會在適當時使用 "throwonerror" 旗標,或使用 EncoderExceptionFallback 和 DecoderExceptionFallback 類別。通常不建議使用自動調整後援,因為這常會導致資料遺失或混淆,而且速度比簡單的字元取代更慢。如果是 ANSI 編碼,則自動調整行為是預設值。
下列程式碼範例會將字串從一個編碼方式轉換成另一個編碼方式。
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode
' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)
' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace
using System;
using System.Text;
namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}
using namespace System;
using namespace System::Text;
int main()
{
String^ unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding^ ascii = Encoding::ASCII;
Encoding^ unicode = Encoding::Unicode;
// Convert the string into a Byte->Item[].
array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );
// Perform the conversion from one encoding to the other.
array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );
// Convert the new Byte into[] a char and[] then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
String^ asciiString = gcnew String( asciiChars );
// Display the strings created before and after the conversion.
Console::WriteLine( "Original String*: {0}", unicodeString );
Console::WriteLine( "Ascii converted String*: {0}", asciiString );
}
package ConvertExample;
import System.*;
import System.Text.*;
class ConvertExampleClass
{
public static void main(String[] args)
{
String unicodeString =
"This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.get_ASCII();
Encoding unicode = Encoding.get_Unicode();
// Convert the string into a byte[].
ubyte unicodeBytes[] = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
ubyte asciiBytes[] = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char asciiChars[] = new
char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.length, asciiChars, 0);
String asciiString = new String(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
} //main
} //ConvertExampleClass
System..::.Object
System.Text..::.Encoding
System.Text..::.ASCIIEncoding
System.Text..::.UnicodeEncoding
System.Text..::.UTF32Encoding
System.Text..::.UTF7Encoding
System.Text..::.UTF8Encoding
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
支援版本:3.5、2.0、1.0
XNA Framework
支援版本:2.0、1.0
參考
其他資源