ASCIIEncoding: GetByteCount

GetByteCount

Calculates the number of bytes produced by encoding the characters in the specified string or character array.



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

Parameters

Chars
[ByRef] Variant. The string or character array containg the 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 characters.

Remarks

To calculate the exact array size required by GetBytes 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.

ExceptionCondition
ArgumentNullException Chars is null.
ArgumentOutOfRangeException Index is less than the LBound of a character array or less than zero for a string.
-or-
Count is less than zero. -or-
Index and Count do not denote a valid range in Chars.
EncoderFallbackException 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 ASCIIEncoding.

    Dim s           As String
    Dim Chars()     As Integer
    Dim ASCII       As New ASCIIEncoding
    Dim ByteCount   As Long
    
    s = "ASCII Encoding Example"
    ByteCount = ASCII.GetByteCount(s)
    Debug.Print CorString.Format("{0} bytes needed to encode string.", ByteCount)
    Debug.Print
    
    Chars = NewChars(ChrW$(&H23), ChrW$(&H25), ChrW$(&H3A0), ChrW$(&H3A3))
    ByteCount = ASCII.GetByteCount(Chars, 1, 2)
    Debug.Print CorString.Format("{0} bytes need to encode characters.", ByteCount)
End Sub

' This code produces the following output.
'
'   22 bytes needed to encode string.
'
'   2 bytes need to encode characters.

See Also

Project CorLib Overview

Class ASCIIEncoding Overview

Encoding

GetBytes

GetMaxByteCount

GetEncoder