UnicodeEncoding.GetEncoder 메서드

정의

유니코드 문자 시퀀스를 UTF-16으로 인코딩된 바이트 시퀀스로 변환하는 인코더를 가져옵니다.

public:
 override System::Text::Encoder ^ GetEncoder();
public override System.Text.Encoder GetEncoder ();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Text.Encoder GetEncoder ();
override this.GetEncoder : unit -> System.Text.Encoder
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetEncoder : unit -> System.Text.Encoder
Public Overrides Function GetEncoder () As Encoder

반환

Encoder

유니코드 문자 시퀀스를 UTF-16으로 인코딩된 바이트 시퀀스로 변환하는 Encoder 개체입니다.

특성

예제

다음 예제에서는 인코더와 디코더를 사용하여 문자열을 바이트 배열로 인코딩한 다음 바이트를 문자 배열로 디코딩합니다.

using namespace System;
using namespace System::Text;
int main()
{
   
   // Get an encoder and a decoder from UnicodeEncoding.
   UnicodeEncoding^ u16 = gcnew UnicodeEncoding( false,true,true );
   Encoder^ myEnc = u16->GetEncoder();
   Decoder^ myDec = u16->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 61 00 06 03 FD 01 B2 03
Using the decoder       : za??�

*/
using System;
using System.Text;

public class SamplesUnicodeEncoding  {

   public static void Main()  {

      // Get an encoder and a decoder from UnicodeEncoding.
      UnicodeEncoding u16 = new UnicodeEncoding( false, true, true );
      Encoder myEnc = u16.GetEncoder();
      Decoder myDec = u16.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 61 00 06 03 FD 01 B2 03
Using the decoder       : za??ß

*/
Imports System.Text

Public Class SamplesUnicodeEncoding   

   Public Shared Sub Main()

      ' Get an encoder and a decoder from UnicodeEncoding.
      Dim u16 As New UnicodeEncoding(False, True, True)
      Dim myEnc As Encoder = u16.GetEncoder()
      Dim myDec As Decoder = u16.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 61 00 06 03 FD 01 B2 03
'Using the decoder       : za??ß

설명

이 메서드는 Encoder.GetBytes 이 클래스의 메서드와 유사한 방식으로 순차적 문자 블록을 바이트 순차 블록으로 GetBytes 변환합니다. 그러나 개체는 Encoder 블록에 걸쳐 있는 문자 시퀀스를 올바르게 인코딩할 수 있도록 호출 간에 상태 정보를 유지 관리합니다. 또한 개체는 Encoder 데이터 블록의 끝에 후행 문자를 유지하고 다음 인코딩 작업에서 후행 문자를 사용합니다. 예를 들어, 데이터 블록이 일치 하지 않는 상위 서로게이트를 종료 하 고 일치 하는 하위 서로게이트는 다음 데이터 블록에 있을 수 있습니다. 따라서 GetDecoderGetEncoder 는 네트워크 전송 및 파일 작업에 유용 합니다. 이러한 작업은 대개 전체 데이터 스트림 대신 데이터 블록을 처리 하기 때문입니다.

오류 검색을 사용하도록 설정 throwOnInvalidBytes 하면 생성자의 매개 변수가 설정 true되고, 이 메서드에서 반환된 개체에서도 Encoder 오류 검색이 사용됩니다. 오류 검색을 사용하도록 설정하고 잘못된 시퀀스를 발견하면 인코더 상태가 정의되지 않고 처리가 중지되어야 합니다.

적용 대상

추가 정보