Encoding: GetMaxCharCount

GetMaxCharCount

Returns the maximum number of characters than can be decoded from the number of bytes specified.



 Public Function GetMaxCharCount(
	  ByVal ByteCount As Long ) As Long

Parameters

ByteCount
[ByVal] Long. The number of bytes to be decoded.

Return Values

Long -  The maximum number of characters that can be decoded from the specified number of bytes.

Remarks

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

GetMaxCharCount retrieves a worst-case number, including the worst case for the currently selected DecoderFallback. If a fallback is chosen with a potentially large string, GetMaxCharCount retrieves large values.

In most cases, this method retrieves reasonable numbers for small strings. For large strings, you might have to choose between using very large buffers and catching errors in the rare case that a more reasonable buffer is too small. You might also want to consider a different approach using GetCharCount or Decoder.Convert.

GetMaxCharCount has no relation to GetBytes. If your application needs a similar function to use with GetBytes, it should use GetMaxByteCount.

When using GetMaxCharCount, your application should allocate the output buffer based on the maximum size of the input buffer. If the output buffer is constrained in size, the application might use the Convert method.

Note that GetMaxCharCount considers the worst case for leftover bytes from a previous encoder operation. For most code pages, passing a value of 0 to this method retrieves values greater than or equal to 1.

Note
GetMaxCharCount(N) is not necessarily the same value as N* GetMaxCharCount(1).
Note to Implementers

All Encoding implementations must guarantee that no buffer overflow exceptions occur if buffers are sized according to the results of this methods calculations.

ExceptionCondition
ArgumentOutOfRangeException ByteCount is less than zero.
DecoderFallbackException A fallback occurrecd
-and-
DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example encodes a string into an array of bytes, and then decodes the bytes into an array of characters.

Public Sub Main()
    Dim U32LE As Encoding
    Dim U32BE As Encoding
    Dim MyStr As String
    Dim ArrBE() As Byte
    Dim ArrLE() As Byte
    
    Set Console.OutputEncoding = Encoding.UTF8
    
    ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-ending byte order.
    Set U32LE = Encoding.GetEncoding("utf-32")
    Set U32BE = Encoding.GetEncoding("utf-32BE")
    
    ' Use a string containing the following characters:
    '    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)
    MyStr = t("za\u0306\u01FD\u03B2")
    
    ' Encode the string using the big-endian byte order.
    ArrBE = U32BE.GetBytes(MyStr)
    
    ' Encode the string using the little-endian byte order.
    ArrLE = U32LE.GetBytes(MyStr)
    
    ' Get the char counts, and decode the byte arrays.
    Console.WriteValue "BE array with BE encoding : "
    PrintCountsAndChars ArrBE, U32BE
    Console.WriteValue "LE array with LE encoding : "
    PrintCountsAndChars ArrLE, U32LE
    
    Console.ReadKey
End Sub

Private Sub PrintCountsAndChars(ByRef Bytes() As Byte, ByVal Enc As Encoding)
    Dim CC As Long
    Dim MCC As Long
    Dim Chars() As Integer
    
    ' Display the name of the encoding used.
    Console.WriteValue "{0,-25} :", Enc.ToString
    
    ' Display the exact character count.
    CC = Enc.GetCharCount(Bytes)
    Console.WriteValue " {0,-3}", CC
    
    ' Display the maximum character count.
    MCC = Enc.GetMaxCharCount(CorArray.Length(Bytes))
    Console.WriteValue " {0,-3} :", MCC
    
    ' Decode the bytes and display the characters.
    Chars = Enc.GetChars(Bytes)
    Console.WriteLine NewString(Chars)
End Sub

' This code produces the following output.
'
'    BE array with BE encoding : CorLib.UTF32Encoding      : 5   12  :zăǽß
'    LE array with LE encoding : CorLib.UTF32Encoding      : 5   12  :zăǽß

See Also

Project CorLib Overview

Class Encoding Overview

GetChars

GetString

GetCharCount

GetDecoder