Encoder.GetByteCount 方法

定義

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

多載

GetByteCount(ReadOnlySpan<Char>, Boolean)

在衍生類別中覆寫時,計算藉由編碼 'chars' 範圍中一組字元所產生的位元組數。 參數會指示,在計算之後是否要清除編碼器的內部狀態。

GetByteCount(Char*, Int32, Boolean)

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

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

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

備註

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

若要計算儲存所產生位元組所需的確切陣列大小 GetBytes ,應用程式應該使用 GetByteCount

如果 GetBytes 呼叫 時 flushfalse 編碼器會將尾端字元儲存在內部緩衝區中的資料區塊結尾,並在下一個編碼作業中使用。 應用程式應該在相同區塊上呼叫之前,立即在資料區塊上呼叫 GetByteCountGetBytes ,以便計算中包含上一個區塊中的任何尾端字元。

GetByteCount(ReadOnlySpan<Char>, Boolean)

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

在衍生類別中覆寫時,計算藉由編碼 'chars' 範圍中一組字元所產生的位元組數。 參數會指示,在計算之後是否要清除編碼器的內部狀態。

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

傳回

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

適用於

GetByteCount(Char*, Int32, Boolean)

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

重要

此 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);
[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

參數

chars
Char*

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

count
Int32

要編碼的字元數。

flush
Boolean

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

傳回

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

屬性

例外狀況

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

count 小於零。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 EncoderExceptionFallback

另請參閱

適用於

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

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

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

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

另請參閱

適用於