UnicodeEncoding.GetDecoder 方法

定義

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

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

傳回

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

範例

下列範例使用編碼器和解碼器將字串編碼為位元組陣列,然後將位元組解碼為字元陣列。

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??ß

備註

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

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

適用於

另請參閱