Encoding 類別

定義

表示字元編碼方式。

public ref class Encoding abstract
public ref class Encoding abstract : ICloneable
public abstract class Encoding
public abstract class Encoding : ICloneable
[System.Serializable]
public abstract class Encoding
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoding : ICloneable
type Encoding = class
type Encoding = class
    interface ICloneable
[<System.Serializable>]
type Encoding = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Encoding = class
    interface ICloneable
Public MustInherit Class Encoding
Public MustInherit Class Encoding
Implements ICloneable
繼承
Encoding
衍生
屬性
實作

範例

下列範例會將字串從一個編碼轉換成另一個編碼。

注意

數位是 byte[] 這個範例中唯一包含編碼數據的型別。 .NET CharString 類型本身為 Unicode,因此 GetChars 呼叫會將數據譯碼回 Unicode。

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 array.
   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.
   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 );
}
// 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 (?)
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 備註

建構函式

Encoding()

初始化 Encoding 類別的新執行個體。

Encoding(Int32)

初始化對應到指定字碼頁之 Encoding 類別的新執行個體。

Encoding(Int32, EncoderFallback, DecoderFallback)

使用指定的編碼器和解碼器後援策略,初始化對應到指定字碼頁之 Encoding 類別的新執行個體。

屬性

ASCII

取得 ASCII (7 位元) 字元集 (Character Set) 的編碼方式。

BigEndianUnicode

取得 UTF-16 格式的編碼方式,其使用由大到小的位元組順序。

BodyName

在衍生類別中覆寫時,取得可以與郵件代理程式主體標籤一起使用的目前編碼方式名稱。

CodePage

在衍生類別中覆寫時,取得目前 Encoding 的字碼頁識別項。

DecoderFallback

取得或設定目前 DecoderFallback 物件的 Encoding 物件。

Default

取得此 .NET 實作的預設編碼。

EncoderFallback

取得或設定目前 EncoderFallback 物件的 Encoding 物件。

EncodingName

在衍生類別中覆寫時,取得目前編碼方式的人們可讀取 (Human-Readable) 的描述。

HeaderName

在衍生類別中覆寫時,取得可以與郵件代理程式標頭標籤一起使用的目前編碼方式名稱。

IsBrowserDisplay

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來顯示內容。

IsBrowserSave

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來儲存內容。

IsMailNewsDisplay

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來顯示內容。

IsMailNewsSave

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來儲存內容。

IsReadOnly

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否為唯讀。

IsSingleByte

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否使用單一位元組字碼指標。

Latin1

取得 Latin1 字元集的編碼方式, (ISO-8859-1) 。

Preamble

在衍生類別中覆寫時,傳回範圍,其包含指定所用編碼方式的位元組序列。

Unicode

以位元組由小到大位元組順序取得 UTF-16 格式的編碼方式。

UTF32

以位元組由小到大位元組順序取得 UTF-32 格式的編碼方式。

UTF7
已淘汰.

取得 UTF-7 格式的編碼方式。

UTF8

取得 UTF-8 格式的編碼方式。

WebName

在衍生類別中覆寫時,若要取得目前的編碼方式,請取得向 Internet Assigned Numbers Authority (IANA) 註冊的名稱。

WindowsCodePage

在衍生類別中覆寫時,請取得最能符合目前編碼方式的 Windows 作業系統字碼頁。

方法

Clone()

在衍生類別中覆寫時,會建立目前 Encoding 物件的淺層複本。

Convert(Encoding, Encoding, Byte[])

將整個位元組陣列從一種編碼方式轉換成另一種編碼方式。

Convert(Encoding, Encoding, Byte[], Int32, Int32)

將位元組陣列中的某一位元組範圍由一種編碼方式轉換成另一種編碼方式。

CreateTranscodingStream(Stream, Encoding, Encoding, Boolean)

Stream建立 ,用來在內部Encoding與外部 Encoding之間轉碼資料,類似於 Convert(Encoding, Encoding, Byte[])

Equals(Object)

判斷指定的 Object 和目前的執行個體是否相等。

GetByteCount(Char*, Int32)

在衍生類別中覆寫時,計算從指定的字元指標開始,編碼一組字元所產生的位元組數目。

GetByteCount(Char[])

在衍生類別中覆寫時,計算編碼指定字元陣列中所有字元所產生的位元組數目。

GetByteCount(Char[], Int32, Int32)

在衍生類別中覆寫時,計算從指定的字元陣列編碼一組字元所產生的位元組數目。

GetByteCount(ReadOnlySpan<Char>)

在衍生類別中覆寫時,計算藉由編碼指定字元範圍中字元所產生的位元組數。

GetByteCount(String)

在衍生類別中覆寫時,計算編碼指定的字串字元所產生的位元組數目。

GetByteCount(String, Int32, Int32)

在衍生類別中覆寫時,計算藉由從指定的字串編碼一組字元所產生的位元組數。

GetBytes(Char*, Int32, Byte*, Int32)

在衍生類別中覆寫時,從指定字元指標開始將一組字元編碼成位元組序列 (會從指定的位元組指標開始存放這些位元組)。

GetBytes(Char[])

在衍生類別中覆寫時,將指定字元陣列中的所有字元編碼成位元組序列。

GetBytes(Char[], Int32, Int32)

在衍生類別中覆寫時,將指定字元陣列中的一組字元編碼成位元組序列。

GetBytes(Char[], Int32, Int32, Byte[], Int32)

在衍生類別中覆寫時,將指定字元陣列中的一組字元編碼成指定的位元組陣列。

GetBytes(ReadOnlySpan<Char>, Span<Byte>)

在衍生類別中覆寫時,從指定的唯讀範圍將一組字元編碼到位元組範圍。

GetBytes(String)

在衍生類別中覆寫時,將指定字串中的所有字元編碼成位元組序列。

GetBytes(String, Int32, Int32)

在衍生類別中覆寫時,從指定字串中指定的 index 開始,將 count 指定的字元數編碼到位元組陣列。

GetBytes(String, Int32, Int32, Byte[], Int32)

在衍生類別中覆寫時,將指定字串中的一組字元編碼成指定的位元組陣列。

GetCharCount(Byte*, Int32)

在衍生類別中覆寫時,計算從指定的位元組指標開始,解碼位元組序列所產生的字元數目。

GetCharCount(Byte[])

在衍生類別中覆寫時,計算解碼指定位元組陣列中所有位元組所產生的字元數目。

GetCharCount(Byte[], Int32, Int32)

在衍生類別中覆寫時,計算從指定的位元組陣列解碼位元組序列所產生的字元數目。

GetCharCount(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,計算藉由解碼所提供唯讀位元組範圍時產生的字元數。

GetChars(Byte*, Int32, Char*, Int32)

在衍生類別中覆寫時,從指定位元組指標開始將位元組序列解碼成一組字元 (會從指定的字元指標開始存放這些字元)。

GetChars(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成一組字元。

GetChars(Byte[], Int32, Int32)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列解碼成一組字元。

GetChars(Byte[], Int32, Int32, Char[], Int32)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列解碼成指定的字元陣列。

GetChars(ReadOnlySpan<Byte>, Span<Char>)

在衍生類別中覆寫時,將指定唯讀位元組範圍中的所有位元組解碼成字元範圍。

GetDecoder()

在衍生類別中覆寫時,取得會將編碼的位元組序列轉換成字元序列的解碼器。

GetEncoder()

在衍生類別中覆寫時,取得會將 Unicode 字元序列轉換成編碼的位元組序列的編碼器。

GetEncoding(Int32)

傳回與指定字碼頁識別項相關聯的編碼方式。

GetEncoding(Int32, EncoderFallback, DecoderFallback)

傳回與指定字碼頁識別項相關聯的編碼方式。 參數會針對無法編碼的字元以及無法解碼的位元組序列指定錯誤處理常式。

GetEncoding(String)

傳回與指定字碼頁名稱相關聯的編碼方式。

GetEncoding(String, EncoderFallback, DecoderFallback)

傳回與指定字碼頁名稱相關聯的編碼方式。 參數會針對無法編碼的字元以及無法解碼的位元組序列指定錯誤處理常式。

GetEncodings()

傳回包含所有編碼方式的陣列。

GetHashCode()

傳回目前執行個體的雜湊碼。

GetMaxByteCount(Int32)

在衍生類別中覆寫時,計算編碼指定的字元數所產生的最大位元組數目。

GetMaxCharCount(Int32)

在衍生類別中覆寫時,計算解碼指定的位元組數目所產生的最大字元數目。

GetPreamble()

在衍生類別中覆寫時,傳回可指定所用編碼方式的位元組序列。

GetString(Byte*, Int32)

在衍生類別中覆寫時,將指定位址開頭之指定數目的位元組解碼為字串。

GetString(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成字串。

GetString(Byte[], Int32, Int32)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列解碼成字串。

GetString(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將指定位元組範圍中的所有位元組解碼成字串。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsAlwaysNormalized()

取得值,指出目前的編碼方式是否永遠都是使用預設的正規化表單進行正規化。

IsAlwaysNormalized(NormalizationForm)

在衍生類別中覆寫時取得值,指出目前的編碼方式是否永遠都是使用指定的正規化表單進行正規化。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RegisterProvider(EncodingProvider)

註冊編碼提供者。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TryGetBytes(ReadOnlySpan<Char>, Span<Byte>, Int32)

如果目的地夠大,則從指定的只讀範圍將一組字元編碼為位元組範圍。

TryGetChars(ReadOnlySpan<Byte>, Span<Char>, Int32)

如果目的地夠大,則會將字元範圍從指定的唯讀範圍譯碼為一組位元組。

擴充方法

GetBytes(Encoding, ReadOnlySequence<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 編碼為 Byte 陣列。

GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 byte,並將結果寫入到 writer

GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 編碼為 byte,並將結果輸出到 bytes

GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySpan<T> 編碼為 byte,並將結果寫入到 writer

GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 char,並將結果寫入到 writer

GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 char,並將結果輸出至 chars

GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>)

使用指定的 Encoding,將指定的 ReadOnlySpan<T> 解碼為 char,並將結果寫入到 writer

GetString(Encoding, ReadOnlySequence<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 String

適用於

另請參閱