Encoder.GetByteCount 方法

定義

當在衍生類別中覆寫時,會計算編碼一組字元所產生的位元組數。

多載

名稱 Description
GetByteCount(ReadOnlySpan<Char>, Boolean)

當在衍生類別中覆寫時,會計算出在「char」區間編碼一組字元所產生的位元組數。 一個參數表示計算後是否要清除編碼器的內部狀態。

GetByteCount(Char*, Int32, Boolean)

當在派生類別中覆寫時,會計算從指定字元指標開始編碼一組字元所產生的位元組數。 一個參數表示計算後是否要清除編碼器的內部狀態。

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

當在派生類別中覆寫時,會計算從指定字元陣列編碼一組字元所產生的位元組數。 一個參數表示計算後是否要清除編碼器的內部狀態。

備註

此方法不影響編碼器的狀態。

為了計算儲存所得位元組所需的精確陣列大小 GetBytes ,應用程式應使用 GetByteCount

GetBytes以設定為 false時呼叫flush,編碼器會將資料區塊末尾的尾部字元儲存在內部緩衝區,並在下一次編碼操作中使用這些字元。 應用程式應在呼叫GetBytes同一區塊前立即呼叫GetByteCount該資料區塊,使前一個區塊的後續字元被納入計算中。

GetByteCount(ReadOnlySpan<Char>, Boolean)

當在衍生類別中覆寫時,會計算出在「char」區間編碼一組字元所產生的位元組數。 一個參數表示計算後是否要清除編碼器的內部狀態。

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>

一個字元的 span 要編碼。

flush
Boolean

true模擬計算後編碼器內部狀態的清除;否則,。 false

傳回

編碼指定字元及內部緩衝區中任意字元所產生的位元組數。

適用於

GetByteCount(Char*, Int32, Boolean)

重要

此 API 不符合 CLS 規範。

當在派生類別中覆寫時,會計算從指定字元指標開始編碼一組字元所產生的位元組數。 一個參數表示計算後是否要清除編碼器的內部狀態。

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

參數

chars
Char*

一個指向第一個要編碼字元的指標。

count
Int32

需要編碼的字元數。

flush
Boolean

true模擬計算後編碼器內部狀態的清除;否則,。 false

傳回

編碼指定字元及內部緩衝區中任意字元所產生的位元組數。

屬性

例外狀況

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

傳回

編碼指定字元及內部緩衝區中任意字元所產生的位元組數。

例外狀況

charsnull

indexcount 小於零。

-或-

indexcount 不表示在 中的 chars有效範圍。

後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼

-及-

Fallback 設定為 EncoderExceptionFallback

範例

以下程式碼範例示範如何使用 GetByteCount Unicode Encoder回傳編碼字元陣列所需的位元組數。

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.
'

另請參閱

適用於