Läs på engelska Redigera

Dela via


Encoder.GetByteCount Method

Definition

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters.

Overloads

GetByteCount(ReadOnlySpan<Char>, Boolean)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters in the 'chars' span. A parameter indicates whether to clear the internal state of the encoder after the calculation.

GetByteCount(Char*, Int32, Boolean)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. A parameter indicates whether to clear the internal state of the encoder after the calculation.

GetByteCount(Char[], Int32, Int32, Boolean)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. A parameter indicates whether to clear the internal state of the encoder after the calculation.

Remarks

This method does not affect the state of the encoder.

To calculate the exact array size that GetBytes requires to store the resulting bytes, the application should use GetByteCount.

If GetBytes is called with flush set to false, the encoder stores trailing characters at the end of the data block in an internal buffer and uses them in the next encoding operation. The application should call GetByteCount on a block of data immediately before calling GetBytes on the same block, so that any trailing characters from the previous block are included in the calculation.

GetByteCount(ReadOnlySpan<Char>, Boolean)

Source:
Encoder.cs
Source:
Encoder.cs
Source:
Encoder.cs

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters in the 'chars' span. A parameter indicates whether to clear the internal state of the encoder after the calculation.

C#
public virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);

Parameters

chars
ReadOnlySpan<Char>

A character span to encode.

flush
Boolean

true to simulate clearing the internal state of the encoder after the calculation; otherwise, false.

Returns

The number of bytes produced by encoding the specified characters and any characters in the internal buffer.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

GetByteCount(Char*, Int32, Boolean)

Source:
Encoder.cs
Source:
Encoder.cs
Source:
Encoder.cs

Important

This API is not CLS-compliant.

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. A parameter indicates whether to clear the internal state of the encoder after the calculation.

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

Parameters

chars
Char*

A pointer to the first character to encode.

count
Int32

The number of characters to encode.

flush
Boolean

true to simulate clearing the internal state of the encoder after the calculation; otherwise, false.

Returns

The number of bytes produced by encoding the specified characters and any characters in the internal buffer.

Attributes

Exceptions

chars is null (Nothing in Visual Basic .NET).

count is less than zero.

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

-and-

Fallback is set to EncoderExceptionFallback.

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET 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 2.0, 2.1

GetByteCount(Char[], Int32, Int32, Boolean)

Source:
Encoder.cs
Source:
Encoder.cs
Source:
Encoder.cs

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. A parameter indicates whether to clear the internal state of the encoder after the calculation.

C#
public abstract int GetByteCount(char[] chars, int index, int count, bool flush);

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.

flush
Boolean

true to simulate clearing the internal state of the encoder after the calculation; otherwise, false.

Returns

The number of bytes produced by encoding the specified characters and any characters in the internal buffer.

Exceptions

chars is null.

index or count is less than zero.

-or-

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

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

-and-

Fallback is set to EncoderExceptionFallback.

Examples

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

C#
using System;
using System.Text;

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

        Encoder uniEncoder = Encoding.Unicode.GetEncoder();
        int byteCount = uniEncoder.GetByteCount(chars, 0, chars.Length, true);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

/* This example produces the following output.

8 bytes needed to encode characters.

*/

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.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