ASCIIEncoding.GetCharCount Method

Definition

Overloads

GetCharCount(Byte[], Int32, Int32)

Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

GetCharCount(Byte*, Int32)

Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

GetCharCount(Byte[], Int32, Int32)

Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

public override int GetCharCount (byte[] bytes, int index, int count);
Parameters
bytes
Byte[]

The byte array containing the sequence of bytes to decode.

index
Int32

The index of the first byte to decode.

count
Int32

The number of bytes to decode.

Returns

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

Exceptions

bytes is null.

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

-or-

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

A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example demonstrates how to use the GetCharCount method to return the number of characters produced by decoding a range of elements in a byte array.

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {65,83,67,73,73,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int charCount = ascii->GetCharCount( bytes, 6, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}

using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();
        int charCount = ascii.GetCharCount(bytes, 6, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}
Imports System
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim bytes() As Byte = { _
             65,  83,  67,  73,  73,  32,  69, _
            110,  99, 111, 100, 105, 110, 103, _
             32,  69, 120,  97, 109, 112, 108, 101}
      
        Dim ascii As New ASCIIEncoding()
        Dim charCount As Integer = ascii.GetCharCount(bytes, 6, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

Remarks

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

GetCharCount(Byte*, Int32)

This API is not CLS-compliant.

CLS-compliant alternative
System.Text.ASCIIEncoding.GetCharCount(Byte[], Int32, Int32)

Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public override int GetCharCount (byte* bytes, int count);
Parameters
bytes
Byte*

A pointer to the first byte to decode.

count
Int32

The number of bytes to decode.

Returns

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

Exceptions

bytes is null.

count is less than zero.

-or-

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

A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Remarks

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