Export (0) Print
Expand All

ASCIIEncoding.GetMaxCharCount Method

Calculates the maximum number of characters produced by decoding the specified number of bytes.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public override int GetMaxCharCount (
	int byteCount
)
public int GetMaxCharCount (
	int byteCount
)
public override function GetMaxCharCount (
	byteCount : int
) : int
Not applicable.

Parameters

byteCount

The number of bytes to decode.

Return Value

The maximum number of characters produced by decoding the specified number of bytes.

Exception typeCondition

ArgumentOutOfRangeException

byteCount is less than zero.

-or-

The resulting number of bytes is greater than the maximum number that can be returned as an integer.

To calculate the exact array size required by GetChars to store the resulting characters, your application should use 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.

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, then GetMaxCharCount can return large values.

If the application is using a decoder fallback with a maximum fallback length of n, GetMaxCharCount retrieves n*x instead of x. For example, if the application is using a decoder fallback and sets a constructor of DecoderReplacementFallback("???"), GetMaxCharCount retrieves 3*x.

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 Convert.

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

NoteNote:

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

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.

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
        );
    }
}

import System.*;
import System.Text.*;

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft