Export (0) Print
Expand All

ASCIIEncoding.GetMaxCharCount Method

Calculates the maximum number of characters that can result from decoding a specified number of bytes.

[Visual Basic]
Overrides Public Function GetMaxCharCount( _
   ByVal byteCount As Integer _
) As Integer
[C#]
public override int GetMaxCharCount(
 int byteCount
);
[C++]
public: int GetMaxCharCount(
 int byteCount
);
[JScript]
public override function GetMaxCharCount(
   byteCount : int
) : int;

Parameters

byteCount
The number of bytes to decode.

Return Value

The maximum number of characters that can result from decoding byteCount number of bytes.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException byteCount is less than zero.

Remarks

Use this method to determine a maximum buffer size for character arrays passed to ASCIIEncoding.GetChars for the current encoding. Using this maximum buffer size helps ensure that buffer overflow exceptions do not occur.

Use GetCharCount to calculate exactly, or GetMaxCharCount to calculate at most, the array size required by the GetChars method to store decoded bytes.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetMaxCharCount method to calculate the maximum number of characters needed to decode a specified number of bytes.

[Visual Basic] 
Imports System
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim ascii As New ASCIIEncoding()
        Dim byteCount As Integer = 8
        Dim maxCharCount As Integer = ascii.GetMaxCharCount(byteCount)
        Console.WriteLine( _
            "Maximum of {0} characters needed to decode {1} bytes.", _
            maxCharCount, _
            byteCount _
        )
    End Sub
End Class

[C#] 
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = 8;
        int maxCharCount = ascii.GetMaxCharCount(byteCount);
        Console.WriteLine(
            "Maximum of {0} characters needed to decode {1} bytes.",
            maxCharCount,
            byteCount
        );
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main() 
{
   ASCIIEncoding* ascii = new ASCIIEncoding();
   int byteCount = 8;
   int maxCharCount = ascii -> GetMaxCharCount(byteCount);
   Console::WriteLine(S"Maximum of {0} characters needed to decode {1} bytes.",
      __box(maxCharCount), __box(byteCount));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

ASCIIEncoding Class | ASCIIEncoding Members | System.Text Namespace | GetChars | GetChars

Show:
© 2014 Microsoft