Decoder.GetChars 方法

定义

在派生类中重写时,将一个字节序列解码为一组字符。

重载

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

在派生类中重写时,将范围字节序列和内部缓冲区中的任何字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。

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

在派生类中重写时,将字节序列(从指定的字节指针处开始)和任何内部缓冲区中的字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。

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

在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。

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

在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。 一个参数,指示转换后是否要清除解码器的内部状态。

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

在派生类中重写时,将范围字节序列和内部缓冲区中的任何字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。

public:
 virtual int GetChars(ReadOnlySpan<System::Byte> bytes, Span<char> chars, bool flush);
public virtual int GetChars (ReadOnlySpan<byte> bytes, Span<char> chars, bool flush);
abstract member GetChars : ReadOnlySpan<byte> * Span<char> * bool -> int
override this.GetChars : ReadOnlySpan<byte> * Span<char> * bool -> int
Public Overridable Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char), flush As Boolean) As Integer

参数

bytes
ReadOnlySpan<Byte>

要解码的字节范围。

chars
Span<Char>

要写入生成的字符集的范围。

flush
Boolean

如果要在转换后清除解码器的内部状态,则为 true;否则,为 false

返回

Int32

在由 chars 参数指示的范围处写入的实际字符数。

注解

请记住, Decoder 对象在调用 之间保存状态 GetChars 。 当应用程序使用数据流完成时,它应将 参数设置为 , flush true 以确保刷新状态信息。 使用此设置时,解码器将忽略数据块末尾的无效字节并清除内部缓冲区。

若要计算存储生成的字符所需的确切范围大小, GetChars 应用程序应使用 GetCharCount

如果在设置为 时调用 ,则解码器将尾随字节存储在内部缓冲区中数据块末尾,并用于下一个 GetChars flush false 解码操作。 应用程序应在对同一块调用 之前立即对数据块调用 ,以便计算中包含上一块的任何 GetCharCount GetChars 尾随字节。

如果应用程序要转换输入流的多个段,请考虑使用 Convert 方法。 GetChars 如果输出范围不够大,则 会引发异常,但会填充尽可能多的空间并返回读取的字节和写入的字符,只要输出数组允许至少 Convert 两个字符。 有关更多 Encoding.GetChars 注释,另请参阅主题。

适用于

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

重要

此 API 不符合 CLS。

在派生类中重写时,将字节序列(从指定的字节指针处开始)和任何内部缓冲区中的字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。

public:
 virtual int GetChars(System::Byte* bytes, int byteCount, char* chars, int charCount, bool flush);
[System.CLSCompliant(false)]
public virtual int GetChars (byte* bytes, int byteCount, char* chars, int charCount, bool flush);
public virtual int GetChars (byte* bytes, int byteCount, char* chars, int charCount, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetChars (byte* bytes, int byteCount, char* chars, int charCount, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual int GetChars (byte* bytes, int byteCount, char* chars, int charCount, bool flush);
[<System.CLSCompliant(false)>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int * bool -> int

参数

bytes
Byte*

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

byteCount
Int32

要解码的字节数。

chars
Char*

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

charCount
Int32

要写入的最大字符数。

flush
Boolean

如果要在转换后清除解码器的内部状态,则为 true;否则,为 false

返回

Int32

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

属性

例外

bytesnull (Nothing)。

  • 或 -

charsnull (Nothing)。

byteCountcharCount 小于零。

charCount 少于所产生的字符数。

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

-和-

Fallback 设置为 DecoderExceptionFallback

注解

请记住, Decoder 对象在调用 之间保存状态 GetChars 。 当应用程序使用数据流完成时,它应将 参数设置为 , flush true 以确保刷新状态信息。 使用此设置时,解码器将忽略数据块末尾的无效字节并清除内部缓冲区。

若要计算存储生成的字符所需的确切缓冲区大小, GetChars 应用程序应使用 GetCharCount

如果在设置为 时调用 ,则解码器将尾随字节存储在内部缓冲区中数据块末尾,并用于下一个 GetChars flush false 解码操作。 应用程序应在对同一块调用 之前立即对数据块调用 ,以便计算中包含上一块的任何 GetCharCount GetChars 尾随字节。

如果应用程序要转换输入流的多个段,请考虑使用 Convert 方法。 GetChars 如果输出缓冲区不够大,但将填充尽可能多的空间并返回读取的字节和写入的字符,则会引发异常,只要输出数组允许至少 Convert 两个字符。 有关更多 Encoding.GetChars 注释,另请参阅主题。

另请参阅

适用于

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

在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。

public:
 abstract int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
abstract member GetChars : byte[] * int * int * char[] * int -> int
Public MustOverride 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

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

返回

Int32

写入 chars 的实际字符数。

例外

bytesnull (Nothing)。

  • 或 -

charsnull (Nothing)。

byteIndexbyteCountcharIndex 小于零。

  • 或 -

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

  • 或 -

charIndex 不是 chars 中的有效索引。

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

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

-和-

Fallback 设置为 DecoderExceptionFallback

示例

下面的示例演示如何解码字节数组中的一系列元素,以及如何将它们存储在 Unicode 字符数组中。 GetCharCount方法用于计算将解码的元素存储在数组 中所需的字符数 bytesGetChars方法对字节数组中的指定元素进行解码,将其存储在新的字符数组中。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<Byte>^bytes = {85,0,110,0,105,0,99,0,111,0,100,0,101,0};
   Decoder^ uniDecoder = Encoding::Unicode->GetDecoder();
   int charCount = uniDecoder->GetCharCount( bytes, 0, bytes->Length );
   chars = gcnew array<Char>(charCount);
   int charsDecodedCount = uniDecoder->GetChars( bytes, 0, bytes->Length, 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();
}

/* This code example produces the following output.

7 characters used to decode bytes.
Decoded chars: [U][n][i][c][o][d][e]

*/
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        Decoder uniDecoder = Encoding.Unicode.GetDecoder();

        int charCount = uniDecoder.GetCharCount(bytes, 0, bytes.Length);
        chars = new Char[charCount];
        int charsDecodedCount = uniDecoder.GetChars(bytes, 0, bytes.Length, 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();
    }
}

/* This code example produces the following output.

7 characters used to decode bytes.
Decoded chars: [U][n][i][c][o][d][e]

*/
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0 _
        }
        
        Dim uniDecoder As Decoder = Encoding.Unicode.GetDecoder()
        
        Dim charCount As Integer = uniDecoder.GetCharCount(bytes, 0, bytes.Length)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = _
            uniDecoder.GetChars(bytes, 0, bytes.Length, 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

'This code example produces the following output.
'
'7 characters used to decode bytes.
'Decoded chars: [U][n][i][c][o][d][e]
'

注解

请记住, Decoder 对象在调用 之间保存状态 GetChars 。 当应用程序使用数据流完成时,它应将 参数设置为 , flush true 以确保刷新状态信息。 使用此设置时,解码器将忽略数据块末尾的无效字节并清除内部缓冲区。

若要计算存储生成的字符所需的确切数组大小, GetChars 应用程序应使用 GetCharCount

如果在设置为 时调用 ,则解码器将尾随字节存储在内部缓冲区中数据块末尾,并用于下一个 GetChars flush false 解码操作。 应用程序应在对同一块调用 之前立即对数据块调用 ,以便计算中包含上一块的任何 GetCharCount GetChars 尾随字节。

如果应用程序要转换输入流的多个段,请考虑使用 Convert 方法。 GetChars 如果输出缓冲区不够大,但将填充尽可能多的空间并返回读取的字节和写入的字符,则会引发异常,只要输出数组允许至少 Convert 两个字符。 有关更多 Encoding.GetChars 注释,另请参阅主题。

另请参阅

适用于

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

在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。 一个参数,指示转换后是否要清除解码器的内部状态。

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

参数

bytes
Byte[]

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

byteIndex
Int32

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

byteCount
Int32

要解码的字节数。

chars
Char[]

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

charIndex
Int32

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

flush
Boolean

如果要在转换后清除解码器的内部状态,则为 true;否则,为 false

返回

Int32

写入 chars 参数的实际字符数。

例外

bytesnull (Nothing)。

  • 或 -

charsnull (Nothing)。

byteIndexbyteCountcharIndex 小于零。

  • 或 -

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

  • 或 -

charIndex 不是 chars 中的有效索引。

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

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

-和-

Fallback 设置为 DecoderExceptionFallback

注解

请记住, Decoder 对象在调用 之间保存状态 GetChars 。 当应用程序使用数据流完成时,它应将 参数设置为 , flush true 以确保刷新状态信息。 使用此设置时,解码器将忽略数据块末尾的无效字节并清除内部缓冲区。

若要计算存储生成的字符所需的确切数组大小, GetChars 应用程序应使用 GetCharCount

如果在设置为 时调用 ,则解码器将尾随字节存储在内部缓冲区中数据块末尾,并用于下一个 GetChars flush false 解码操作。 应用程序应在对同一块调用 之前立即对数据块调用 ,以便计算中包含上一块的任何 GetCharCount GetChars 尾随字节。

如果应用程序要转换输入流的多个段,请考虑使用 Convert 方法。 GetChars 如果输出缓冲区不够大,但将填充尽可能多的空间并返回读取的字节和写入的字符,则会引发异常,只要输出数组允许至少 Convert 两个字符。 有关更多 Encoding.GetChars 注释,另请参阅主题。

另请参阅

适用于