Encoder.GetByteCount 方法

定义

在派生类中重写时,计算对一组字符进行编码所产生的字节数。

重载

GetByteCount(ReadOnlySpan<Char>, Boolean)

在派生类中重写时,计算对“字符”范围中的一组字符进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

GetByteCount(Char*, Int32, Boolean)

在派生类中重写时,计算对一组字符(从指定的字符指针处开始)进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

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

在派生类中重写时,计算对指定字符数组中的一组字符进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

注解

此方法不会影响编码器的状态。

若要计算 GetBytes 存储生成的字节所需的确切数组大小,应用程序应使用 GetByteCount

如果 GetBytesflush 设置为的情况 false 下调用,编码器会将末尾字符存储在内部缓冲区中的数据块的末尾,并在下一个编码操作中使用它们。 应用程序应 GetByteCount 在对同一块调用之前立即调用数据块 GetBytes ,以便在计算中包括前一块中的任何尾随字符。

GetByteCount(ReadOnlySpan<Char>, Boolean)

在派生类中重写时,计算对“字符”范围中的一组字符进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

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

参数

chars
ReadOnlySpan<Char>

要编码的字符范围。

flush
Boolean

若要在计算后模拟清除解码器的内部状态,则为 true;否则为 false

返回

Int32

通过对指定字符和内部缓冲区中的所有字符进行编码而产生的字节数。

适用于

GetByteCount(Char*, Int32, Boolean)

重要

此 API 不符合 CLS。

在派生类中重写时,计算对一组字符(从指定的字符指针处开始)进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

public:
 virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
public virtual int GetByteCount (char* chars, int count, bool flush);
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
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

参数

chars
Char*

指向第一个要编码的字符的指针。

count
Int32

要编码的字符的数目。

flush
Boolean

若要在计算后模拟清除解码器的内部状态,则为 true;否则为 false

返回

Int32

通过对指定字符和内部缓冲区中的所有字符进行编码而产生的字节数。

属性

例外

charsnull(在 Visual Basic .NET 中为 Nothing)。

count 小于零。

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

Fallback 设置为 EncoderExceptionFallback

另请参阅

适用于

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

在派生类中重写时,计算对指定字符数组中的一组字符进行编码所产生的字节数。 一个参数指示计算后是否要清除编码器的内部状态。

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

参数

chars
Char[]

包含要编码的字符集的字符数组。

index
Int32

第一个要编码的字符的索引。

count
Int32

要编码的字符的数目。

flush
Boolean

若要在计算后模拟清除解码器的内部状态,则为 true;否则为 false

返回

Int32

通过对指定字符和内部缓冲区中的所有字符进行编码而产生的字节数。

例外

charsnull

indexcount 小于零。

  • 或 -

indexcount 不表示 chars 中的有效范围。

发生回退(有关详细信息,请参阅采用 .NET 的字符编码

-和-

Fallback 设置为 EncoderExceptionFallback

示例

下面的代码示例演示如何使用 GetByteCount 方法返回使用 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.
'

另请参阅

适用于