ASCIIEncoding.GetByteCount Method

Definition

Calculates the number of bytes produced by encoding a set of characters.

Overloads

GetByteCount(ReadOnlySpan<Char>)

Calculates the number of bytes produced by encoding the specified character span.

GetByteCount(String)

Calculates the number of bytes produced by encoding the characters in the specified String.

GetByteCount(Char*, Int32)

Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

GetByteCount(Char[], Int32, Int32)

Calculates the number of bytes produced by encoding a set of characters from the specified character array.

GetByteCount(ReadOnlySpan<Char>)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

Calculates the number of bytes produced by encoding the specified character span.

C#
public override int GetByteCount(ReadOnlySpan<char> chars);

Parameters

chars
ReadOnlySpan<Char>

The span that contains the set of characters to encode.

Returns

The number of bytes produced by encoding the specified character span.

Remarks

To calculate the exact size required by GetBytes to store the resulting bytes, use GetByteCount. To calculate the maximum size, use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10

GetByteCount(String)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

Calculates the number of bytes produced by encoding the characters in the specified String.

C#
public override int GetByteCount(string chars);

Parameters

chars
String

The String containing the set of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Exceptions

chars is null.

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

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

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode a string using ASCIIEncoding.

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        String chars = "ASCII Encoding Example";

        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = ascii.GetByteCount(chars);
        Console.WriteLine(
            "{0} bytes needed to encode string.", byteCount
        );
    }
}

Remarks

To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.

See also

Applies to

.NET 10 and other versions
Product Versions
.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.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetByteCount(Char*, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Text.ASCIIEncoding.GetByteCount(Char[], Int32, Int32)

Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount(char* chars, int count);
C#
[System.CLSCompliant(false)]
public override int GetByteCount(char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount(char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount(char* chars, int count);

Parameters

chars
Char*

A pointer to the first character to encode.

count
Int32

The number of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Attributes

Exceptions

chars 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 (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

Remarks

To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.

See also

Applies to

.NET 10 and other versions
Product Versions
.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

GetByteCount(Char[], Int32, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

Calculates the number of bytes produced by encoding a set of characters from the specified character array.

C#
public override int GetByteCount(char[] chars, int index, int count);

Parameters

chars
Char[]

The character array containing the set of characters to encode.

index
Int32

The index of the first character to encode.

count
Int32

The number of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Exceptions

chars is null.

index or count is less than zero.

-or-

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

-or-

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

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

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode an array of Unicode characters using ASCIIEncoding.

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = ascii.GetByteCount(chars, 1, 2);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

Remarks

To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.

See also

Applies to

.NET 10 and other versions
Product Versions
.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.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0