UTF8Encoding.GetCharCount Method

Definition

Calculates the number of characters produced by decoding a sequence of bytes.

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(ReadOnlySpan<Byte>)

Calculates the number of characters produced by decoding the specified byte span.

GetCharCount(Byte[], Int32, Int32)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

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

C#
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.

Error detection is enabled, and bytes contains an invalid sequence of bytes.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

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

C#
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();
        int charCount = utf8.GetCharCount(bytes, 2, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}

Remarks

To calculate the exact array size required by GetChars to store the resulting characters, call the GetCharCount method. To calculate the maximum array size, call the GetMaxCharCount method. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw an ArgumentException exception. Without error detection, invalid sequences are ignored, and no exception is thrown.

See also

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCharCount(Byte*, Int32)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

Important

This API is not CLS-compliant.

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

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetCharCount(byte* bytes, int count);
C#
[System.CLSCompliant(false)]
public override int GetCharCount(byte* bytes, int count);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount(byte* bytes, int count);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
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.

Attributes

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.

Error detection is enabled, and bytes contains an invalid sequence of bytes.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Remarks

To calculate the exact array size required by GetChars to store the resulting characters, call the GetCharCount method. To calculate the maximum array size, call the GetMaxCharCount method. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw an ArgumentException exception. Without error detection, invalid sequences are ignored, and no exception is thrown.

See also

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCharCount(ReadOnlySpan<Byte>)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

Calculates the number of characters produced by decoding the specified byte span.

C#
public override int GetCharCount(ReadOnlySpan<byte> bytes);

Parameters

bytes
ReadOnlySpan<Byte>

The span containing the set of bytes to decode.

Returns

The number of characters produced by decoding the specified byte span.

Remarks

To calculate the exact size required by GetChars to store the resulting characters, call the GetCharCount method. To calculate the maximum size, call the GetMaxCharCount method. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw an ArgumentException exception. Without error detection, invalid sequences are ignored, and no exception is thrown.

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10