Encoder.GetByteCount Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
public:
virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);
public virtual int GetByteCount (ReadOnlySpan<char> chars, bool flush);
abstract member GetByteCount : ReadOnlySpan<char> * bool -> int
override this.GetByteCount : ReadOnlySpan<char> * bool -> int
Public Overridable Function GetByteCount (chars As ReadOnlySpan(Of Char), flush As Boolean) As Integer
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
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.
public:
virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
public virtual int GetByteCount (char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetByteCount (char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual int GetByteCount (char* chars, int count, bool flush);
[<System.CLSCompliant(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
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
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.
public:
abstract int GetByteCount(cli::array <char> ^ chars, int index, int count, bool flush);
public abstract int GetByteCount (char[] chars, int index, int count, bool flush);
abstract member GetByteCount : char[] * int * int * bool -> int
Public MustOverride Function GetByteCount (chars As Char(), index As Integer, count As Integer, flush As Boolean) As Integer
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.
using namespace System;
using namespace System::Text;
int main()
{
// Unicode characters.
// Pi
// Sigma
array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
Encoder^ uniEncoder = Encoding::Unicode->GetEncoder();
int byteCount = uniEncoder->GetByteCount( chars, 0, chars->Length, true );
Console::WriteLine( "{0} bytes needed to encode characters.", byteCount );
}
/* This code example produces the following output.
8 bytes needed to encode characters.
*/
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.
*/
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class EncoderExample
Public Shared Sub Main()
' Unicode characters.
' ChrW(35) = #
' ChrW(37) = %
' ChrW(928) = Pi
' ChrW(931) = Sigma
Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
Dim uniEncoder As Encoder = Encoding.Unicode.GetEncoder()
Dim byteCount As Integer = _
uniEncoder.GetByteCount(chars, 0, chars.Length, True)
Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
End Sub
End Class
'
'This example produces the following output.
'
'8 bytes needed to encode characters.
'