共用方式為


UTF7Encoding.GetEncoder 方法

定義

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

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,可以將 Unicode 字元序列轉換成以 UTF-7 編碼的位元組序列。

範例

下列程式碼範例示範如何使用 GetEncoder 方法來取得編碼器,將字元序列轉換成 UTF-7 編碼的位元組序列。

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^ utf7Encoder = Encoding::UTF7->GetEncoder();
   int byteCount = utf7Encoder->GetByteCount( chars, 2, 3, true );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf7Encoder->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 UTF7EncodingExample {
    public static void Main() {
        Char[] chars = new Char[] {'a', 'b', 'c', '\u0300', '\ua0a0'};
        Byte[] bytes;

        Encoder utf7Encoder = Encoding.UTF7.GetEncoder();

        int byteCount = utf7Encoder.GetByteCount(chars, 2, 3, true);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7Encoder.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 UTF7EncodingExample
    
    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 utf7Encoder As Encoder = Encoding.UTF7.GetEncoder()
        
        Dim byteCount As Integer = utf7Encoder.GetByteCount(chars, 2, 3, True)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7Encoder.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

備註

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

適用於

另請參閱