UTF8Encoding.GetChars 方法

定义

将一个字节序列解码为一组字符。Decodes a sequence of bytes into a set of characters.

重载

GetChars(ReadOnlySpan<Byte>, Span<Char>)

将指定的字节范围解码为指定的字符范围。Decodes the specified byte span into the specified character span.

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

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer.

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

将指定字节数组中的一个字节序列解码为指定的字符数组。Decodes a sequence of bytes from the specified byte array into the specified character array.

GetChars(ReadOnlySpan<Byte>, Span<Char>)

将指定的字节范围解码为指定的字符范围。Decodes the specified byte span into the specified character span.

public:
 override int GetChars(ReadOnlySpan<System::Byte> bytes, Span<char> chars);
public override int GetChars (ReadOnlySpan<byte> bytes, Span<char> chars);
override this.GetChars : ReadOnlySpan<byte> * Span<char> -> int
Public Overrides Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char)) As Integer

参数

bytes
ReadOnlySpan<Byte>

包含要解码的字节的范围。The span containing the bytes to decode.

chars
Span<Char>

要包含生成的字符集的范围。The span to contain the resulting set of characters.

返回

Int32

写入 chars 的实际字符数。The actual number of characters written into chars.

注解

若要计算存储所生成的字符所需的准确大小 GetChars ,请调用 GetCharCount 方法。To calculate the exact size required by GetChars to store the resulting characters, call the GetCharCount method. 若要计算最大大小,请调用 GetMaxCharCount 方法。To calculate the maximum size, call the GetMaxCharCount method. GetCharCount方法通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 如果未检测到错误,将忽略无效的序列,且不会引发异常。Without error detection, invalid sequences are ignored, and no exception is thrown.

如果要解码的字节集包含字节顺序标记 (BOM) 并且非 BOM 感知类型的方法返回的字节范围为,则该字符 U+FFFE 将包含在此方法返回的字符范围内。If the set of bytes to be decoded includes the byte order mark (BOM) and the span of bytes was returned by a method of a non-BOM aware type, the character U+FFFE is included in the span of characters returned by this method. 可以通过调用方法将其移除 String.TrimStartYou can remove it by calling the String.TrimStart method.

要转换的数据(如从流中读取的数据)只能在顺序块中使用。Data to be converted, such as data read from a stream, might be available only in sequential blocks. 在这种情况下,或者如果数据量很大以致需要分为较小块,请 Decoder 分别使用或 Encoder 方法提供的对象或 GetDecoder GetEncoder 方法。In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, use the Decoder or the Encoder object provided by the GetDecoder method or the GetEncoder method, respectively.

适用于

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

重要

此 API 不符合 CLS。

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer.

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);
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(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
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int

参数

bytes
Byte*

指向第一个要解码的字节的指针。A pointer to the first byte to decode.

byteCount
Int32

要解码的字节数。The number of bytes to decode.

chars
Char*

一个指针,指向开始写入所产生的字符集的位置。A pointer to the location at which to start writing the resulting set of characters.

charCount
Int32

要写入的最大字符数。The maximum number of characters to write.

返回

Int32

在由 chars 指示的位置处写入的实际字符数。The actual number of characters written at the location indicated by chars.

属性

例外

bytesnullbytes is null.

-or- charsnullchars is null.

byteCountcharCount 小于零。byteCount or charCount is less than zero.

启用了错误检测,并且 bytes 包含无效的字节序列。Error detection is enabled, and bytes contains an invalid sequence of bytes.

-or- charCount 少于所产生的字符数。charCount is less than the resulting number of characters.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET) -和--and- DecoderFallback 设置为 DecoderExceptionFallbackDecoderFallback is set to DecoderExceptionFallback.

注解

若要计算存储所生成的字符所需的确切数组大小 GetChars ,请调用 GetCharCount 方法。To calculate the exact array size required by GetChars to store the resulting characters, call the GetCharCount method. 若要计算最大数组大小,请调用 GetMaxCharCount 方法。To calculate the maximum array size, call the GetMaxCharCount method. GetCharCount方法通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 如果未检测到错误,将忽略无效的序列,且不会引发异常。Without error detection, invalid sequences are ignored, and no exception is thrown.

如果要解码的字节范围包含字节顺序标记 (BOM) 并且字节数组由非 BOM 感知类型的方法返回,则此方法返回的字符数组中包含字符 U + FFFE。If the range of bytes to be decoded includes the byte order mark (BOM) and the byte array was returned by a method of a non-BOM aware type, the character U+FFFE is included in the character array returned by this method. 可以通过调用方法将其移除 String.TrimStartYou can remove it by calling the String.TrimStart method.

要转换的数据(如从流中读取的数据)只能在顺序块中使用。Data to be converted, such as data read from a stream, might be available only in sequential blocks. 在这种情况下,或者如果数据量很大以致需要分为较小块,请 Decoder 分别使用或 Encoder 方法提供的对象或 GetDecoder GetEncoder 方法。In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, use the Decoder or the Encoder object provided by the GetDecoder method or the GetEncoder method, respectively.

另请参阅

适用于

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

将指定字节数组中的一个字节序列解码为指定的字符数组。Decodes a sequence of bytes from the specified byte array into the specified character array.

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[]

包含要解码的字节序列的字节数组。The byte array containing the sequence of bytes to decode.

byteIndex
Int32

第一个要解码的字节的索引。The index of the first byte to decode.

byteCount
Int32

要解码的字节数。The number of bytes to decode.

chars
Char[]

要用于包含所产生的字符集的字符数组。The character array to contain the resulting set of characters.

charIndex
Int32

开始写入所产生的字符集的索引位置。The index at which to start writing the resulting set of characters.

返回

Int32

写入 chars 的实际字符数。The actual number of characters written into chars.

例外

bytesnullbytes is null.

-or- charsnullchars is null.

byteIndexbyteCountcharIndex 小于零。byteIndex or byteCount or charIndex is less than zero.

-or- byteindexbyteCount 不表示 bytes中的有效范围。byteindex and byteCount do not denote a valid range in bytes.

-or- charIndex 不是 chars 中的有效索引。charIndex is not a valid index in chars.

启用了错误检测,并且 bytes 包含无效的字节序列。Error detection is enabled, and bytes contains an invalid sequence of bytes.

-or- chars 中从 charIndex 到数组结尾没有足够容量来容纳所产生的字符。chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET) -和--and- DecoderFallback 设置为 DecoderExceptionFallbackDecoderFallback is set to DecoderExceptionFallback.

示例

下面的示例使用 GetChars 方法对字节数组中的一系列元素进行解码,并将结果存储在字符数组中。The following example uses the GetChars method to decode a range of elements in a byte array and store the result in a character array.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<Byte>^bytes = {85,84,70,56,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int charCount = utf8->GetCharCount( bytes, 2, 13 );
   chars = gcnew array<Char>(charCount);
   int charsDecodedCount = utf8->GetChars( bytes, 2, 13, chars, 0 );
   Console::WriteLine( "{0} characters used to decode bytes.", charsDecodedCount );
   Console::Write( "Decoded chars: " );
   IEnumerator^ myEnum = chars->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Char c = safe_cast<Char>(myEnum->Current);
      Console::Write( "[{0}]", c.ToString() );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();

        int charCount = utf8.GetCharCount(bytes, 2, 13);
        chars = new Char[charCount];
        int charsDecodedCount = utf8.GetChars(bytes, 2, 13, chars, 0);

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);
        }
        Console.WriteLine();
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 13)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = utf8.GetChars(bytes, 2, 13, chars, 0)
        
        Console.WriteLine("{0} characters used to decode bytes.", charsDecodedCount)
        
        Console.Write("Decoded chars: ")
        Dim c As Char
        For Each c In  chars
            Console.Write("[{0}]", c)
        Next c
        Console.WriteLine()
    End Sub
End Class

注解

若要计算存储所生成的字符所需的确切数组大小 GetChars ,请调用 GetCharCount 方法。To calculate the exact array size required by GetChars to store the resulting characters, call the GetCharCount method. 若要计算最大数组大小,请调用 GetMaxCharCount 方法。To calculate the maximum array size, call the GetMaxCharCount method. GetCharCount方法通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。With error detection, an invalid sequence causes this method to throw an ArgumentException exception. 如果未检测到错误,将忽略无效的序列,且不会引发异常。Without error detection, invalid sequences are ignored, and no exception is thrown.

如果要解码的字节范围包含字节顺序标记 (BOM) 并且字节数组由非 BOM 感知类型的方法返回,则此方法返回的字符数组中包含字符 U + FFFE。If the range of bytes to be decoded includes the byte order mark (BOM) and the byte array was returned by a method of a non-BOM aware type, the character U+FFFE is included in the character array returned by this method. 可以通过调用方法将其移除 String.TrimStartYou can remove it by calling the String.TrimStart method.

要转换的数据(如从流中读取的数据)只能在顺序块中使用。Data to be converted, such as data read from a stream, might be available only in sequential blocks. 在这种情况下,或者如果数据量很大以致需要分为较小块,请 Decoder Encoder 分别使用 GetDecoder 方法或方法提供的或 GetEncoderIn this case, or if the amount of data is so large that it needs to be divided into smaller blocks, use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

另请参阅

适用于