UTF32Encoding.GetChars 方法

定义

将一个字节序列解码为一组字符。

重载

GetChars(Byte*, Int32, Char*, Int32)

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。

GetChars(Byte[], Int32, Int32, Char[], Int32)

将指定字节数组中的一个字节序列解码为指定的字符数组。

GetChars(Byte*, Int32, Char*, Int32)

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs

重要

此 API 不符合 CLS。

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。

public:
 override int GetChars(System::Byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int

参数

bytes
Byte*

指向第一个要解码的字节的指针。

byteCount
Int32

要解码的字节数。

chars
Char*

一个指针,指向开始写入所产生的字符集的位置。

charCount
Int32

要写入的最大字符数。

返回

在由 chars 指示的位置处写入的实际字符数。

属性

例外

bytesnull

charsnull

byteCountcharCount 小于零。

启用了错误检测,并且 bytes 包含无效的字节序列。

charCount 少于所产生的字符数。

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

DecoderFallback 设置为 DecoderExceptionFallback

注解

若要计算存储生成的字符所需的 GetChars 确切数组大小,请 GetCharCount 调用 方法。 若要计算最大数组大小,请调用 GetMaxCharCount 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

使用错误检测时,无效序列会导致此方法引发 ArgumentException。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

如果要解码的字节范围包括 BOM) (字节顺序标记,并且字节数组是由非 BOM 感知类型的方法返回的,则字符 U+FFFE 包含在此方法返回的字符数组中。 可以通过调用 String.TrimStart 方法将其删除。

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者,如果数据量太大,需要将其划分为较小的块,应用程序将分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoderGetEncoder

另请参阅

适用于

GetChars(Byte[], Int32, Int32, Char[], Int32)

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs

将指定字节数组中的一个字节序列解码为指定的字符数组。

public:
 override int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
override this.GetChars : byte[] * int * int * char[] * int -> int
Public Overrides Function GetChars (bytes As Byte(), byteIndex As Integer, byteCount As Integer, chars As Char(), charIndex As Integer) As Integer

参数

bytes
Byte[]

包含要解码的字节序列的字节数组。

byteIndex
Int32

第一个要解码的字节的索引。

byteCount
Int32

要解码的字节数。

chars
Char[]

要用于包含所产生的字符集的字符数组。

charIndex
Int32

开始写入所产生的字符集的索引位置。

返回

写入 chars 的实际字符数。

例外

bytesnull

charsnull

byteIndexbyteCountcharIndex 小于零。

byteindexbyteCount 不表示 bytes中的有效范围。

charIndex 不是 chars 中的有效索引。

启用了错误检测,并且 bytes 包含无效的字节序列。

chars 中从 charIndex 到数组结尾没有足够容量来容纳所产生的字符。

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

DecoderFallback 设置为 DecoderExceptionFallback

示例

下面的示例将字符串编码为一个字节数组,然后将这些字节解码为一个字符数组。

using namespace System;
using namespace System::Text;
void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc );
int main()
{
   
   // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
   UTF32Encoding^ u32LE = gcnew UTF32Encoding( false,true,true );
   UTF32Encoding^ u32BE = gcnew UTF32Encoding( true,true,true );
   
   // Create byte arrays from the same string containing the following characters:
   //    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)
   String^ myStr = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
   
   // barrBE uses the big-endian byte order.
   array<Byte>^barrBE = gcnew array<Byte>(u32BE->GetByteCount( myStr ));
   u32BE->GetBytes( myStr, 0, myStr->Length, barrBE, 0 );
   
   // barrLE uses the little-endian byte order.
   array<Byte>^barrLE = gcnew array<Byte>(u32LE->GetByteCount( myStr ));
   u32LE->GetBytes( myStr, 0, myStr->Length, barrLE, 0 );
   
   // Get the char counts and decode the byte arrays.
   Console::Write( "BE array with BE encoding : " );
   PrintCountsAndChars( barrBE, u32BE );
   Console::Write( "LE array with LE encoding : " );
   PrintCountsAndChars( barrLE, u32LE );
   
   // Decode the byte arrays using an encoding with a different byte order.
   Console::Write( "BE array with LE encoding : " );
   try
   {
      PrintCountsAndChars( barrBE, u32LE );
   }
   catch ( System::ArgumentException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

   Console::Write( "LE array with BE encoding : " );
   try
   {
      PrintCountsAndChars( barrLE, u32BE );
   }
   catch ( System::ArgumentException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc )
{
   
   // Display the name of the encoding used.
   Console::Write( "{0,-25} :", enc );
   
   // Display the exact character count.
   int iCC = enc->GetCharCount( bytes );
   Console::Write( " {0,-3}", iCC );
   
   // Display the maximum character count.
   int iMCC = enc->GetMaxCharCount( bytes->Length );
   Console::Write( " {0,-3} :", iMCC );
   
   // Decode the bytes and display the characters.
   array<Char>^chars = gcnew array<Char>(iCC);
   enc->GetChars( bytes, 0, bytes->Length, chars, 0 );
   Console::WriteLine( chars );
}

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

BE array with BE encoding : System.Text.UTF32Encoding : 7   14  :za??�?
LE array with LE encoding : System.Text.UTF32Encoding : 7   14  :za??�?
BE array with LE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.
LE array with BE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.

*/
using System;
using System.Text;

public class SamplesUTF32Encoding  {

   public static void Main()  {

      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      UTF32Encoding u32LE = new UTF32Encoding( false, true, true );
      UTF32Encoding u32BE = new UTF32Encoding( true, true, true );

      // Create byte arrays from the same string containing the following characters:
      //    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)
      //    a high-surrogate value (U+D8FF)
      //    a low-surrogate value (U+DCFF)
      String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";

      // barrBE uses the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
      u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );

      // barrLE uses the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
      u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );

      // Get the char counts and decode the byte arrays.
      Console.Write( "BE array with BE encoding : " );
      PrintCountsAndChars( barrBE, u32BE );
      Console.Write( "LE array with LE encoding : " );
      PrintCountsAndChars( barrLE, u32LE );

      // Decode the byte arrays using an encoding with a different byte order.
      Console.Write( "BE array with LE encoding : " );
      try  {
         PrintCountsAndChars( barrBE, u32LE );
      }
      catch ( System.ArgumentException e )  {
         Console.WriteLine( e.Message );
      }

      Console.Write( "LE array with BE encoding : " );
      try  {
         PrintCountsAndChars( barrLE, u32BE );
      }
      catch ( System.ArgumentException e )  {
         Console.WriteLine( e.Message );
      }
   }

   public static void PrintCountsAndChars( byte[] bytes, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-25} :", enc.ToString() );

      // Display the exact character count.
      int iCC  = enc.GetCharCount( bytes );
      Console.Write( " {0,-3}", iCC );

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount( bytes.Length );
      Console.Write( " {0,-3} :", iMCC );

      // Decode the bytes and display the characters.
      char[] chars = new char[iCC];
      enc.GetChars( bytes, 0, bytes.Length, chars, 0 );
      Console.WriteLine( chars );
   }
}
Imports System.Text

Public Class SamplesUTF32Encoding   

   Public Shared Sub Main()

      ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Dim u32LE As New UTF32Encoding(False, True, True)
      Dim u32BE As New UTF32Encoding(True, True, True)


      ' Create byte arrays from the same string containing the following characters:
      '    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)
      '    a high-surrogate value (U+D8FF)
      '    a low-surrogate value (U+DCFF)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)

      ' barrBE uses the big-endian byte order.
      ' 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 barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
      u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)

      ' barrLE uses the little-endian byte order.
      ' 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 barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
      u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)


      ' Get the char counts and decode the byte arrays.
      Console.Write("BE array with BE encoding : ")
      PrintCountsAndChars(barrBE, u32BE)
      Console.Write("LE array with LE encoding : ")
      PrintCountsAndChars(barrLE, u32LE)


      ' Decode the byte arrays using an encoding with a different byte order.
      Console.Write("BE array with LE encoding : ")
      Try
         PrintCountsAndChars(barrBE, u32LE)
      Catch e As System.ArgumentException
         Console.WriteLine(e.Message)
      End Try

      Console.Write("LE array with BE encoding : ")
      Try
         PrintCountsAndChars(barrLE, u32BE)
      Catch e As System.ArgumentException
         Console.WriteLine(e.Message)
      End Try

   End Sub


   Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)

      ' Display the name of the encoding used.
      Console.Write("{0,-25} :", enc.ToString())

      ' Display the exact character count.
      Dim iCC As Integer = enc.GetCharCount(bytes)
      Console.Write(" {0,-3}", iCC)

      ' Display the maximum character count.
      Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
      Console.Write(" {0,-3} :", iMCC)

      ' Decode the bytes and display the characters.
      Dim chars(iCC) As Char
      enc.GetChars(bytes, 0, bytes.Length, chars, 0)
      Console.WriteLine(chars)

   End Sub

End Class

注解

若要计算存储生成的字符所需的 GetChars 确切数组大小,请 GetCharCount 调用 方法。 若要计算最大数组大小,请调用 GetMaxCharCount 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

使用错误检测时,无效序列会导致此方法引发 ArgumentException。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

如果要解码的字节范围包括 BOM) (字节顺序标记,并且字节数组是由非 BOM 感知类型的方法返回的,则字符 U+FFFE 包含在此方法返回的字符数组中。 可以通过调用 String.TrimStart 方法将其删除。

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者,如果数据量太大,需要将其划分为较小的块,应用程序将分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoderGetEncoder

另请参阅

适用于