Edit

Share via


Decoder.GetChars Method

Definition

When overridden in a derived class, decodes a sequence of bytes into a set of characters.

Overloads

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

When overridden in a derived class, decodes a sequence of span bytes and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array.

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

When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

Source:
Decoder.cs
Source:
Decoder.cs
Source:
Decoder.cs

When overridden in a derived class, decodes a sequence of span bytes and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

Parameters

bytes
ReadOnlySpan<Byte>

A byte span to decode.

chars
Span<Char>

A span to write the resulting set of characters.

flush
Boolean

true to clear the internal state of the decoder after the conversion; otherwise, false.

Returns

The actual number of characters written at the span indicated by the chars parameter.

Remarks

Remember that the Decoder object saves state between calls to GetChars. When the application is done with a stream of data, it should set the flush parameter to true to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.

To calculate the exact span size that GetChars requires to store the resulting characters, the application should use GetCharCount.

If GetChars is called with flush set to false, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call GetCharCount on a block of data immediately before calling GetChars on the same block, so that any trailing bytes from the previous block are included in the calculation.

If your application is to convert many segments of an input stream, consider using the Convert method. GetChars will throw an exception if the output span isn't large enough, but Convert will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the Encoding.GetChars topic for more comments.

Applies to

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

Source:
Decoder.cs
Source:
Decoder.cs
Source:
Decoder.cs

Important

This API is not CLS-compliant.

When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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);
[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
[<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

Parameters

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.

flush
Boolean

true to clear the internal state of the decoder after the conversion; otherwise, false.

Returns

The actual number of characters written at the location indicated by the chars parameter.

Attributes

Exceptions

bytes is null (Nothing).

-or-

chars is null (Nothing).

byteCount or charCount is less than zero.

charCount is less than the resulting number of characters.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

Fallback is set to DecoderExceptionFallback.

Remarks

Remember that the Decoder object saves state between calls to GetChars. When the application is done with a stream of data, it should set the flush parameter to true to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.

To calculate the exact buffer size that GetChars requires to store the resulting characters, the application should use GetCharCount.

If GetChars is called with flush set to false, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call GetCharCount on a block of data immediately before calling GetChars on the same block, so that any trailing bytes from the previous block are included in the calculation.

If your application is to convert many segments of an input stream, consider using the Convert method. GetChars will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the Encoding.GetChars topic for more comments.

See also

Applies to

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

Source:
Decoder.cs
Source:
Decoder.cs
Source:
Decoder.cs

When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array.

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

Parameters

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.

Returns

The actual number of characters written into chars.

Exceptions

bytes is null (Nothing).

-or-

chars is null (Nothing).

byteIndex or byteCount or charIndex is less than zero.

-or-

byteindex and byteCount do not denote a valid range in bytes.

-or-

charIndex is not a valid index in chars.

chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

Fallback is set to DecoderExceptionFallback.

Examples

The following example demonstrates how to decode a range of elements from a byte array and store them in a Unicode character array. The GetCharCount method is used to calculate the number of characters needed to store the decoded elements in the array bytes. The GetChars method decodes the specified elements in the byte array and stores them in the new character array.

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

Remarks

Remember that the Decoder object saves state between calls to GetChars. When the application is done with a stream of data, it should set the flush parameter to true to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.

To calculate the exact array size that GetChars requires to store the resulting characters, the application should use GetCharCount.

If GetChars is called with flush set to false, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call GetCharCount on a block of data immediately before calling GetChars on the same block, so that any trailing bytes from the previous block are included in the calculation.

If your application is to convert many segments of an input stream, consider using the Convert method. GetChars will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the Encoding.GetChars topic for more comments.

See also

Applies to

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

Source:
Decoder.cs
Source:
Decoder.cs
Source:
Decoder.cs

When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

Parameters

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.

flush
Boolean

true to clear the internal state of the decoder after the conversion; otherwise, false.

Returns

The actual number of characters written into the chars parameter.

Exceptions

bytes is null (Nothing).

-or-

chars is null (Nothing).

byteIndex or byteCount or charIndex is less than zero.

-or-

byteindex and byteCount do not denote a valid range in bytes.

-or-

charIndex is not a valid index in chars.

chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

Fallback is set to DecoderExceptionFallback.

Remarks

Remember that the Decoder object saves state between calls to GetChars. When the application is done with a stream of data, it should set the flush parameter to true to make sure that the state information is flushed. With this setting, the decoder ignores invalid bytes at the end of the data block and clears the internal buffer.

To calculate the exact array size that GetChars requires to store the resulting characters, the application should use GetCharCount.

If GetChars is called with flush set to false, the decoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next decoding operation. The application should call GetCharCount on a block of data immediately before calling GetChars on the same block, so that any trailing bytes from the previous block are included in the calculation.

If your application is to convert many segments of an input stream, consider using the Convert method. GetChars will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the bytes read and chars written, provided the output array allows for at least two characters. Also see the Encoding.GetChars topic for more comments.

See also

Applies to