UTF8Encoding: GetCharCount

GetCharCount

Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.



 Public Function GetCharCount(
	  ByRef Bytes ( ) As Byte,
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As Long

Parameters

Bytes
[ByRef] Byte. The byte array containing the sequence of bytes to decode.
Index
[ByRef] Optional. Variant. The index of the first byte to decode.
Count
[ByRef] Optional. Variant. The number of bytes to decode.

Return Values

Long -  The number of characters produced by decoding the specified sequence of bytes.

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.

Exceptions

ExceptionCondition
ArgumentNullException Bytes is uninitialized.
ArgumentOutOfRangeException Index is less than the lower-bound of Bytes.
-or-
Count is less than zero.
-or-
Index and Count do not denote a valid range in Bytes.
-or-
The resulting number of bytes is greater than the maximum number that can be returned as an integer.
ArgumentException Error detection is enabled, and Bytes contains an invalid sequence of bytes.
DecoderFallbackException A fallback occurred
-and-
DecoderFallback is set to DecoderExceptionFallback.html.

Examples

The following example demonstrates how to use the GetCharCount method to return the number of characters produced by decoding a range of elements in a byte array.

Public Sub Main()
    Dim Bytes()     As Byte
    Dim UTF8        As New UTF8Encoding
    Dim CharCount   As Long
    
    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, 8)
    
    Console.WriteLine "{0} characters needed to decode bytes.", CharCount

    Console.ReadKey
End Sub

' This code produces the following output.
'
'    8 characters needed to decode bytes.

See Also

Project CorLib Overview

Class UTF8Encoding Overview

GetCharsEx

GetMaxCharCount

GetDecoder