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
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 该方法将其删除。

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

另请参阅

适用于

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

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
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 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 该方法将其删除。

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

另请参阅

适用于