UTF32Encoding.GetEncoder メソッド

定義

Unicode 文字のシーケンスを UTF-32 でエンコードされたバイト シーケンスに変換するエンコーダーを取得します。

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

戻り値

Unicode 文字のシーケンスを UTF-32 でエンコードされたバイト シーケンスに変換する Encoder

次の例では、エンコーダーとデコーダーを使用して文字列をバイト配列にエンコードし、バイトを文字の配列にデコードします。

using namespace System;
using namespace System::Text;
int main()
{
   
   // Get an encoder and a decoder from UTF32Encoding.
   UTF32Encoding ^ u32 = gcnew UTF32Encoding( false,true,true );
   Encoder^ myEnc = u32->GetEncoder();
   Decoder^ myDec = u32->GetDecoder();
   
   // The characters to encode:
   //    Latin Small Letter Z (U+007A)
   //    Latin Small Letter A (U+0061)
   //    Combining Breve (U+0306)
   //    Latin Small Letter AE With Acute (U+01FD)
   //    Greek Small Letter Beta (U+03B2)
   array<Char>^myChars = gcnew array<Char>(5){
      L'z',L'a',L'\u0306',L'\u01FD',L'\u03B2'
   };
   Console::Write( "The original characters : " );
   Console::WriteLine( myChars );
   
   // Encode the character array.
   int iBC = myEnc->GetByteCount( myChars, 0, myChars.Length, true );
   array<Byte>^myBytes = gcnew array<Byte>(iBC);
   myEnc->GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );
   
   // Print the resulting bytes.
   Console::Write( "Using the encoder       : " );
   for ( int i = 0; i < myBytes.Length; i++ )
      Console::Write( "{0:X2} ", myBytes[ i ] );
   Console::WriteLine();
   
   // Decode the byte array back into an array of characters.
   int iCC = myDec->GetCharCount( myBytes, 0, myBytes.Length, true );
   array<Char>^myDecodedChars = gcnew array<Char>(iCC);
   myDec->GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );
   
   // Print the resulting characters.
   Console::Write( "Using the decoder       : " );
   Console::WriteLine( myDecodedChars );
}

/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The original characters : za??
Using the encoder       : 7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00
Using the decoder       : za??

*/
using System;
using System.Text;

public class SamplesUTF32Encoding  {

   public static void Main()  {

      // Get an encoder and a decoder from UTF32Encoding.
      UTF32Encoding u32 = new UTF32Encoding( false, true, true );
      Encoder myEnc = u32.GetEncoder();
      Decoder myDec = u32.GetDecoder();

      // The characters to encode:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      char[] myChars = new char[5] { 'z', 'a', '\u0306', '\u01FD', '\u03B2' };
      Console.Write( "The original characters : " );
      Console.WriteLine( myChars );

      // Encode the character array.
      int iBC  = myEnc.GetByteCount( myChars, 0, myChars.Length, true );
      byte[] myBytes = new byte[iBC];
      myEnc.GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );

      // Print the resulting bytes.
      Console.Write( "Using the encoder       : " );
      for ( int i = 0; i < myBytes.Length; i++ )
         Console.Write( "{0:X2} ", myBytes[i] );
      Console.WriteLine();

      // Decode the byte array back into an array of characters.
      int iCC  = myDec.GetCharCount( myBytes, 0, myBytes.Length, true );
      char[] myDecodedChars = new char[iCC];
      myDec.GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );

      // Print the resulting characters.
      Console.Write( "Using the decoder       : " );
      Console.WriteLine( myDecodedChars );
   }
}


/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The original characters : za??ß
Using the encoder       : 7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00
Using the decoder       : za??ß

*/
Imports System.Text

Public Class SamplesUTF32Encoding   

   Public Shared Sub Main()

      ' Get an encoder and a decoder from UTF32Encoding.
      Dim u32 As New UTF32Encoding(False, True, True)
      Dim myEnc As Encoder = u32.GetEncoder()
      Dim myDec As Decoder = u32.GetDecoder()

      ' The characters to encode:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2)}
      Console.Write("The original characters : ")
      Console.WriteLine(myChars)

      ' Encode the character array.
      Dim iBC As Integer = myEnc.GetByteCount(myChars, 0, myChars.Length, True)
      ' NOTE: In Visual Basic, arrays contain one extra element by default.
      '       The following line creates an array with the exact number of elements required.
      Dim myBytes(iBC - 1) As Byte
      myEnc.GetBytes(myChars, 0, myChars.Length, myBytes, 0, True)

      ' Print the resulting bytes.
      Console.Write("Using the encoder       : ")
      Dim i As Integer
      For i = 0 To myBytes.Length - 1
         Console.Write("{0:X2} ", myBytes(i))
      Next i
      Console.WriteLine()

      ' Decode the byte array back into an array of characters.
      Dim iCC As Integer = myDec.GetCharCount(myBytes, 0, myBytes.Length, True)
      ' NOTE: In Visual Basic, arrays contain one extra element by default.
      '       The following line creates an array with the exact number of elements required.
      Dim myDecodedChars(iCC - 1) As Char
      myDec.GetChars(myBytes, 0, myBytes.Length, myDecodedChars, 0, True)

      ' Print the resulting characters.
      Console.Write("Using the decoder       : ")
      Console.WriteLine(myDecodedChars)

   End Sub

End Class


'This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.
'
'The original characters : za??ß
'Using the encoder       : 7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00
'Using the decoder       : za??ß

注釈

メソッドは Encoder.GetBytes 、 メソッドと同様の方法で、文字のシーケンシャル ブロックをバイトのシーケンシャル ブロックに GetBytes 変換します。 ただし、 は Encoder 、ブロックにまたがる文字シーケンスを正しくエンコードできるように、呼び出し間の状態情報を保持します。 また、は Encoder データブロックの末尾で末尾の文字を保持し、次のエンコーディング操作で末尾の文字を使用します。 たとえば、データ ブロックの末尾に高サロゲートが一致しない場合や、一致する低サロゲートが次のデータ ブロックに存在する可能性があります。 そのため、 GetDecoderGetEncoder は、ネットワークの転送およびファイル操作に役立ちます。これらの操作は、多くの場合、完全なデータストリームではなく、データのブロックを処理するためです。

エラー検出が有効になっている場合、つまり、 throwOnInvalidCharacters コンストラクターのパラメーターが に true設定されている場合、このメソッドによって返される で Encoder エラー検出も有効になります。 エラー検出が有効で、無効なシーケンスが検出された場合、エンコーダーの状態は未定義であり、処理を停止する必要があります。

適用対象

こちらもご覧ください