Encoding 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表一種字元編碼。
public ref class Encoding abstract
public ref class Encoding abstract : ICloneable
public abstract class Encoding
[System.Serializable]
public abstract class Encoding
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoding : ICloneable
public abstract class Encoding : ICloneable
type Encoding = class
[<System.Serializable>]
type Encoding = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Encoding = class
interface ICloneable
type Encoding = class
interface ICloneable
Public MustInherit Class Encoding
Public MustInherit Class Encoding
Implements ICloneable
- 繼承
-
Encoding
- 衍生
- 屬性
- 實作
範例
以下範例將字串從一種編碼轉換到另一種編碼。
Note
byte[]陣列是本範例中唯一包含編碼資料的類型。 .NET Char 和 String 類型本身就是 Unicode,因此 GetChars 呼叫會將資料解碼回 Unicode。
using System;
using System.Text;
class Example
{
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 array.
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.
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);
}
}
// The example displays the following output:
// Original string: This string contains the unicode character Pi (Π)
// Ascii converted string: This string contains the unicode character Pi (?)
Imports System.Text
Class Example
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 array.
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 array into a char array and then into a string.
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
' The example displays the following output:
' Original string: This string contains the unicode character Pi (Π)
' Ascii converted string: This string contains the unicode character Pi (?)
備註
欲了解更多關於此 API 的資訊,請參閱編碼 補充 API 備註。
建構函式
| 名稱 | Description |
|---|---|
| Encoding() |
初始化 Encoding 類別的新執行個體。 |
| Encoding(Int32, EncoderFallback, DecoderFallback) |
初始化一個對應特定代碼頁的新類別實例 Encoding ,並使用指定的編碼器與解碼器備用策略。 |
| Encoding(Int32) |
初始化對應指定程式碼頁的類別新實例 Encoding 。 |
屬性
| 名稱 | Description |
|---|---|
| ASCII |
會獲得 ASCII(7 位元)字元集的編碼。 |
| BigEndianUnicode |
會得到 UTF-16 格式的編碼,使用大端序位元組序。 |
| BodyName |
當在衍生類別中覆寫時,會獲得一個可用於郵件代理體標籤的當前編碼名稱。 |
| CodePage |
當在衍生類別中被覆寫時,會獲得目前 Encoding的代碼頁識別碼。 |
| DecoderFallback |
取得或設定 DecoderFallback 當前 Encoding 物件的物件。 |
| Default |
它會取得這個 .NET 實作的預設編碼。 |
| EncoderFallback |
取得或設定 EncoderFallback 當前 Encoding 物件的物件。 |
| EncodingName |
當在衍生類別中覆寫時,會得到目前編碼的人類可讀描述。 |
| HeaderName |
當 在衍生類別中被覆寫時,會獲得一個可用於郵件代理標頭標籤的當前編碼名稱。 |
| IsBrowserDisplay |
當 在衍生類別中覆寫時,會獲得一個值,表示瀏覽器客戶端是否可以使用目前的編碼來顯示內容。 |
| IsBrowserSave |
當在衍生類別中覆寫時,會獲得一個值,表示瀏覽器客戶端是否能使用目前的編碼來儲存內容。 |
| IsMailNewsDisplay |
當在派生類別中覆寫時,會獲得一個值,表示目前的編碼是否可用於郵件和新聞用戶端顯示內容。 |
| IsMailNewsSave |
當在衍生類別中覆寫時,會獲得一個值,表示目前的編碼是否能被郵件和新聞客戶端用來儲存內容。 |
| IsReadOnly |
當在衍生類別中覆寫時,會獲得一個值,表示目前編碼是否為唯讀。 |
| IsSingleByte |
當在派生類別中覆寫時,會得到一個值,表示目前編碼是否使用單位元組的碼點。 |
| Preamble |
當在衍生類別中覆寫時,會回傳一個包含指定所用編碼序列的位元組的區間。 |
| Unicode |
會用小端序編碼 UTF-16 格式。 |
| UTF32 |
會用小端序的位元組順序編碼 UTF-32 格式。 |
| UTF7 |
會用 UTF-7 格式的編碼。 |
| UTF8 |
會用 UTF-8 格式的編碼。 |
| WebName |
當在衍生類別中被覆寫時,會獲得目前編碼的名稱,註冊於網際網路號碼分配管理局(IANA)。 |
| WindowsCodePage |
當在衍生類別中覆寫時,會得到與目前編碼最接近的 Windows 作業系統代碼頁。 |
方法
| 名稱 | Description |
|---|---|
| Clone() |
當在衍生類別中覆寫時,會產生目前 Encoding 物件的淺層副本。 |
| Convert(Encoding, Encoding, Byte[], Int32, Int32) |
將位元組陣列中的位元組範圍從一個編碼轉換成另一個。 |
| Convert(Encoding, Encoding, Byte[]) |
將整個位元組陣列從一個編碼轉換成另一個。 |
| Equals(Object) |
判斷指定的 Object 是否等於當前實例。 |
| GetByteCount(Char[], Int32, Int32) |
當在派生類別中覆寫時,會計算從指定字元陣列編碼一組字元所產生的位元組數。 |
| GetByteCount(Char[]) |
當在派生類別中覆寫時,會計算將指定字元陣列中所有字元編碼所產生的位元組數。 |
| GetByteCount(Char*, Int32) |
當在派生類別中覆寫時,會計算從指定字元指標開始編碼一組字元所產生的位元組數。 |
| GetByteCount(ReadOnlySpan<Char>) |
當在派生類別中覆寫時,會計算在指定字元區間內編碼字元所產生的位元組數。 |
| GetByteCount(String, Int32, Int32) |
當在派生類別中覆寫時,會計算從指定字串編碼一組字元所產生的位元組數。 |
| GetByteCount(String) |
當在衍生類別中覆寫時,會計算出編碼指定字串字元所產生的位元組數。 |
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(Char[], Int32, Int32) |
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼成一串位元組。 |
| GetBytes(Char[]) |
當在派生類別中覆寫時,會將指定字元陣列中的所有字元編碼成一串位元組。 |
| GetBytes(Char*, Int32, Byte*, Int32) |
當在派生類別中覆寫時,會將一組從指定字元指標開始的字元編碼成一列位元組,並從指定位元組指標開始儲存。 |
| GetBytes(ReadOnlySpan<Char>, Span<Byte>) |
當在派生類別中覆寫時,會將指定唯讀區間的一組字元編碼到位元組區間。 |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
當在派生類別中覆寫時,會將指定字串中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String, Int32, Int32) |
當在派生類別中覆寫時,會將指定字串中指定的 |
| GetBytes(String) |
當在派生類別中覆寫時,會將指定字串中的所有字元編碼成一串位元組。 |
| GetCharCount(Byte[], Int32, Int32) |
當在派生類別中覆寫時,會計算從指定位元組陣列解碼一連串位元組所產生的字元數。 |
| GetCharCount(Byte[]) |
當在派生類別中覆寫時,會計算解碼指定位元組陣列中所有位元組所產生的字元數。 |
| GetCharCount(Byte*, Int32) |
當在派生類別中覆寫時,會計算從指定位元組指標開始解碼一串位元組所產生的字元數。 |
| GetCharCount(ReadOnlySpan<Byte>) |
當在衍生類別中覆寫時,會計算出解碼所提供的唯讀位元組區間所產生的字元數。 |
| GetChars(Byte[], Int32, Int32, Char[], Int32) |
當在派生類別中覆寫時,會將指定的位元組陣列解碼成指定的字元陣列。 |
| GetChars(Byte[], Int32, Int32) |
當在派生類別中覆寫時,會將指定位元組陣列中的一串位元組解碼成一組字元。 |
| GetChars(Byte[]) |
當在派生類別中覆寫時,會將指定位元組陣列中的所有位元組解碼成一組字元。 |
| GetChars(Byte*, Int32, Char*, Int32) |
當在派生類別中覆寫時,會將從指定位元組指標開始的一串位元組解碼成一組字元,這些字元從指定字元指標開始儲存。 |
| GetChars(ReadOnlySpan<Byte>, Span<Char>) |
當在派生類別中覆寫時,會將指定唯讀位元組區間中的所有位元組解碼為字元區間。 |
| GetDecoder() |
當在衍生類別中覆寫時,會得到一個解碼器,將編碼的位元組序列轉換成一串字元。 |
| GetEncoder() |
當在派生類別中被覆寫時,會得到一個編碼器,將一串 Unicode 字元轉換成編碼好的位元組序列。 |
| GetEncoding(Int32, EncoderFallback, DecoderFallback) |
回傳與指定代碼頁識別碼相關的編碼。 參數指定無法編碼字元及無法解碼的位元組序列的錯誤處理程序。 |
| GetEncoding(Int32) |
回傳與指定代碼頁識別碼相關的編碼。 |
| GetEncoding(String, EncoderFallback, DecoderFallback) |
回傳與指定代碼頁名稱相關的編碼。 參數指定無法編碼字元及無法解碼的位元組序列的錯誤處理程序。 |
| GetEncoding(String) |
回傳與指定代碼頁名稱相關的編碼。 |
| GetEncodings() |
回傳包含所有編碼的陣列。 |
| GetHashCode() |
傳回目前實例的哈希碼。 |
| GetMaxByteCount(Int32) |
當在衍生類別中覆寫時,會計算出編碼指定字元數所產生的最大位元組數。 |
| GetMaxCharCount(Int32) |
當在派生類別中覆寫時,會透過解碼指定位元組數計算出最大字元數。 |
| GetPreamble() |
當在派生類別中覆寫時,會回傳一串位元組,指定所使用的編碼方式。 |
| GetString(Byte[], Int32, Int32) |
當在派生類別中覆寫時,會將指定位元組陣列中的一串位元組解碼成字串。 |
| GetString(Byte[]) |
當在衍生類別中覆寫時,會將指定位元組陣列中的所有位元組解碼成一個字串。 |
| GetString(Byte*, Int32) |
當在派生類別中覆寫時,會將從指定位址開始的指定位元組解碼成字串。 |
| GetString(ReadOnlySpan<Byte>) |
當在派生類別中覆寫時,會將指定位元組區間中的所有位元組解碼成一個字串。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsAlwaysNormalized() |
會得到一個值,表示目前編碼是否總是正規化,使用預設的正規化形式。 |
| IsAlwaysNormalized(NormalizationForm) |
當在派生類別中覆寫時,會得到一個值,表示目前編碼是否總是被正規化,並使用指定的正規化形式。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| RegisterProvider(EncodingProvider) |
註冊一個編碼提供者。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |