UTF8Encoding.GetEncoder 方法

定義

取得可以將 Unicode 字元序列轉換成以 UTF-8 編碼的位元組序列的編碼器。

public:
 override System::Text::Encoder ^ GetEncoder();
public override System.Text.Encoder GetEncoder ();
override this.GetEncoder : unit -> System.Text.Encoder
Public Overrides Function GetEncoder () As Encoder

傳回

Encoder

Encoder,可以將 Unicode 字元序列轉換成以 UTF-8 編碼的位元組序列。

範例

下列範例會 GetEncoder 使用 方法來取得編碼器,將字元序列轉換成 UTF-8 編碼的位元組序列。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars = {'a','b','c',L'\u0300',L'\ua0a0'};
   array<Byte>^bytes;
   Encoder^ utf8Encoder = Encoding::UTF8->GetEncoder();
   int byteCount = utf8Encoder->GetByteCount( chars, 2, 3, true );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf8Encoder->GetBytes( chars, 2, 3, bytes, 0, true );
   Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount );
   Console::Write( "Encoded bytes: " );
   IEnumerator^ myEnum = bytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars = new Char[] {'a', 'b', 'c', '\u0300', '\ua0a0'};
        Byte[] bytes;

        Encoder utf8Encoder = Encoding.UTF8.GetEncoder();

        int byteCount = utf8Encoder.GetByteCount(chars, 2, 3, true);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf8Encoder.GetBytes(chars, 2, 3, bytes, 0, true);

        Console.WriteLine(
            "{0} bytes used to encode characters.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        'Characters:
        ' ChrW(97) = a
        ' ChrW(98) = b
        ' ChrW(99) = c
        ' ChrW(768) = `
        ' ChrW(41120) = valid unicode code point, but not a character
        Dim chars() As Char = {ChrW(97), ChrW(98), ChrW(99), ChrW(768), ChrW(41120)}
        Dim bytes() As Byte
        
        Dim utf8Encoder As Encoder = Encoding.UTF8.GetEncoder()
        
        Dim byteCount As Integer = utf8Encoder.GetByteCount(chars, 2, 3, True)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf8Encoder.GetBytes( _
            chars, 2, 3, bytes, 0, True _
        )
        
        Console.WriteLine("{0} bytes used to encode characters.", bytesEncodedCount)
        
        Console.Write("Encoded bytes: ")
        Dim b As Byte
        For Each b In  bytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
    End Sub
End Class

備註

方法會 Encoder.GetBytes 以類似 方法的方式,將字元的循序區塊轉換成循序位元組區塊 GetBytes 。 不過,會 Encoder 維護呼叫之間的狀態資訊,以便正確編碼跨越區塊的字元序列。 也會 Encoder 保留資料區塊結尾的尾端字元,並在下一個編碼作業中使用尾端字元。 例如,資料區塊可能會以不相符的高 Surrogate 結尾,而相符的低 Surrogate 可能位於下一個資料區塊中。 因此, GetDecoderGetEncoder 對於網路傳輸和檔案作業很有用,因為這些作業通常會處理資料區塊,而不是完整的資料流程。

如果啟用錯誤偵測,也就是說, throwOnInvalidCharacters 建構函式的 參數會設定 true 為 ,也會在此方法傳回的 中 Encoder 啟用錯誤偵測。 如果啟用錯誤偵測且遇到不正確序列,編碼器的狀態為未定義且處理必須停止。

適用於

另請參閱