UTF8Encoding: GetByteCount

GetByteCount

Calculates the number of bytes produced by encoding the characters in the specified String or Integer().



 Public Function GetByteCount(
	  ByRef Chars As Variant,
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As Long

Parameters

Chars
[ByRef] Variant. The Integer array or String containing the set of characters to encode.
Index
[ByRef] Optional. Variant. The index of the first character to encode.
Count
[ByRef] Optional. Variant. The number of characters to encode.

Return Values

Long -  The number of bytes produced by encoding the specified set of characters.

Remarks

To calculate the exact array size required by GetBytesEx to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount 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.

Note
To ensure that the encoded bytes are decoded properly, the application should prefix encoded bytes with a preamble.

Exceptions

ExceptionCondition
ArgumentNullException Chars is a null array.
ArgumentOutOfRangeException Index is less than the lower-bound of Chars.
-or-
Count is less than zero.
-or-
Index and Count do not denote a valid range in Chars.
ArgumentException A fallback occurred
-and-
EncoderFallback is set to EncoderExceptionFallback.

Examples

The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode an array of Unicode characters, using UTF8Encoding.

    GetByteCountForString
    GetByteCountForChars
    GetByteCountForSubChars
End Sub

Private Sub GetByteCountForString()
    Const Chars     As String = "UTF8 Encoding Example"
    Dim UTF8        As New UTF8Encoding
    Dim ByteCount   As Long
    
    ByteCount = UTF8.GetByteCount(Chars)
    
    Debug.Print "GetByteCount for String."
    Debug.Print CorString.Format("{0} bytes needed to encode string.", ByteCount)
    Debug.Print
End Sub

Private Sub GetByteCountForChars()
    Dim Chars()     As Integer
    Dim UTF8        As New UTF8Encoding
    Dim ByteCount   As Long
    
    ' The characters to encode:
    '    Latin Small Letter Z (U+007A)
    '    Latin Small Letter A (U+0061)
    '    Combining Breve (U+0306)
    '    Latin Small Letter AE With Acute (U+01FD)
    '    Greek Small Letter Beta (U+03B2)
    '    a high-surrogate value (U+D8FF)
    '    a low-surrogate value (U+DCFF)
    Chars = NewChars("z", "a", &H306, &H1FD, &H3B2, &HD8FF, &HDCFF)
    
    ByteCount = UTF8.GetByteCount(Chars)
    
    Debug.Print "GetByteCount for character array."
    Debug.Print CorString.Format("{0} bytes needed to encode characters.", ByteCount)
    Debug.Print
End Sub

Private Sub GetByteCountForSubChars()
    Dim Chars()     As Integer
    Dim UTF8        As New UTF8Encoding
    Dim ByteCount   As Long

    ' Unicode characters: #, %, Pi, Sigma
    Chars = NewChars("#", "%", &H3A0, &H3A3)
    
    ByteCount = UTF8.GetByteCount(Chars, 1, 2)
        
    Debug.Print "GetByteCount for character sub-array."
    Debug.Print CorString.Format("{0} bytes needed to encode characters.", ByteCount)
End Sub

' This code example produces the following output.
'
'    GetByteCount for String.
'    21 bytes needed to encode string.
'
'    GetByteCount for character array.
'    12 bytes needed to encode characters.
'
'    GetByteCount for character sub-array.
'    3 bytes needed to encode characters.

See Also

Project CorLib Overview

Class UTF8Encoding Overview

GetBytesEx

GetMaxByteCount

GetEncoder