UTF8Encoding.GetChars 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个字节序列解码为一组字符。
重载
| GetChars(ReadOnlySpan<Byte>, Span<Char>) | 将指定的字节范围解码为指定的字符范围。 | 
| GetChars(Byte*, Int32, Char*, Int32) | 将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。 | 
| GetChars(Byte[], Int32, Int32, Char[], Int32) | 将指定字节数组中的一个字节序列解码为指定的字符数组。 | 
GetChars(ReadOnlySpan<Byte>, Span<Char>)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
将指定的字节范围解码为指定的字符范围。
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> -> intPublic Overrides Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char)) As Integer参数
- bytes
- ReadOnlySpan<Byte>
包含要解码的字节的范围。
返回
写入 chars 的实际字符数。
注解
若要计算存储生成的字符所需的 GetChars 确切大小,请 GetCharCount 调用 方法。 若要计算最大大小,请 GetMaxCharCount 调用 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。
使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。
如果要解码的字节集包括 BOM)  (字节顺序标记,并且字节范围由非 BOM 感知类型的方法返回,则此方法返回的字符范围中将包含该字符 U+FFFE 。 可以通过调用 String.TrimStart 方法将其删除。
要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 方法提供的 GetDecoder 或 EncoderGetEncoder 对象。
适用于
GetChars(Byte*, Int32, Char*, Int32)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.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.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[<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*
指向第一个要解码的字节的指针。
- byteCount
- Int32
要解码的字节数。
- chars
- Char*
一个指针,指向开始写入所产生的字符集的位置。
- charCount
- Int32
要写入的最大字符数。
返回
在由 chars 指示的位置处写入的实际字符数。
- 属性
例外
              byteCount 或 charCount 小于零。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)
-和-
注解
若要计算存储生成的字符所需的 GetChars 确切数组大小,请 GetCharCount 调用 方法。 若要计算最大数组大小,请调用 GetMaxCharCount 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。
使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。
如果要解码的字节范围包括 BOM) (字节顺序标记,并且字节数组是由非 BOM 感知类型的方法返回的,则字符 U+FFFE 包含在此方法返回的字符数组中。 可以通过调用 String.TrimStart 方法将其删除。
要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 方法提供的 GetDecoder 或 EncoderGetEncoder 对象。
另请参阅
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)
适用于
GetChars(Byte[], Int32, Int32, Char[], Int32)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.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 -> intPublic 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 的实际字符数。
例外
              byteIndex、byteCount 或 charIndex 小于零。
或
              byteindex 和 byteCount 不表示 bytes中的有效范围。
或
              charIndex 不是 chars 中的有效索引。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)
-和-
示例
以下示例使用 GetChars 方法解码字节数组中的元素范围,并将结果存储在字符数组中。
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 调用 方法。 若要计算最大数组大小,请调用 GetMaxCharCount 方法。 方法 GetCharCount 通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。
使用错误检测时,无效序列会导致此方法引发 ArgumentException 异常。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。
如果要解码的字节范围包括 BOM) (字节顺序标记,并且字节数组是由非 BOM 感知类型的方法返回的,则字符 U+FFFE 包含在此方法返回的字符数组中。 可以通过调用 String.TrimStart 方法将其删除。
要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoder 或 GetEncoder 。
另请参阅
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)
- GetDecoder()
- GetString(Byte[], Int32, Int32)