UTF8Encoding: GetCharsEx

GetCharsEx

Decodes a sequence of bytes from the specified byte array into the specified character array.



 Public Function GetCharsEx(
	  ByRef Bytes ( ) As Byte,
	  ByVal ByteIndex As Long,
	  ByVal ByteCount As Long,
	  ByRef Chars ( ) As Integer,
	  ByVal CharIndex As Long ) As Long

Parameters

Bytes
[ByRef] Byte. The byte array containing the sequence of bytes to decode.
ByteIndex
[ByVal] Long. The index of the first byte to decode.
ByteCount
[ByVal] Long. The number of bytes to decode.
Chars
[ByRef] Integer. The character array to contain the resulting set of characters.
CharIndex
[ByVal] Long. The index at which to start writing the resulting set of characters.

Return Values

Long -  The actual number of characters written into Chars.

Remarks

To calculate the exact array size required by GetCharsEx to store the resulting characters, the application uses GetCharCount. To calculate the maximum array size, the application should use GetMaxCharCount. The GetCharCount method generally allows allocation of less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw a ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application uses the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

Exceptions

ExceptionCondition
ArgumentNullException Bytes is uninitialized.
-or-
Chars is uninitailized.
ArgumentOutOfRangeException ByteIndex is less than the lower-bound of Bytes.
-or-
CharIndex is less than the lower-bound of Chars.
-or-
ByteCount 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.
ArgumentException Error detection is enabled, and Bytes contains an invalid sequence of bytes.
-or-
Chars does not have enough capacity from CharIndex to the end of the array to accommodate the resulting characters.
DecoderFallbackException A fallback occurred
-and-
DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example demonstrates how to use the GetCharsEx method to decode a range of elements in a byte array and store the result in a character array.

Public Sub Main()
    Dim Bytes()             As Byte
    Dim Chars()             As Integer
    Dim UTF8                As New UTF8Encoding
    Dim CharCount           As Long
    Dim CharsDecodedCount   As Long
    Dim c As Variant
    
    Bytes = NewBytes(85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101)
    
    CharCount = UTF8.GetCharCount(Bytes, 2, 13)
    ReDim Chars(1 To CharCount)
    CharsDecodedCount = UTF8.GetCharsEx(Bytes, 2, 13, Chars, 1)
    
    Console.WriteLine "{0} characters used to decode bytes.", CharsDecodedCount
    
    For Each c In Chars
        Console.WriteValue "[{0:$}]", c
    Next

    Console.WriteLine
    Console.ReadKey
End Sub

' This code produces the following output.
'
'    13 characters used to decode bytes.
'    [F][8][ ][E][n][c][o][d][i][n][g][ ][E]

See Also

Project CorLib Overview

Class UTF8Encoding Overview

GetChars

GetCharCount

GetMaxCharCount

GetDecoder

GetString