閱讀英文

共用方式為


ASCIIEncoding.GetCharCount 方法

定義

計算將位元組序列解碼所產生的字元數。

多載

GetCharCount(Byte*, Int32)

計算將起始於指定位元組指標的位元組序列解碼所產生的字元數。

GetCharCount(Byte[], Int32, Int32)

計算將指定位元組陣列中的位元組序列解碼所產生的字元數。

GetCharCount(ReadOnlySpan<Byte>)

計算解碼指定位元組範圍所產生的字元數目。

GetCharCount(Byte*, Int32)

來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Text.ASCIIEncoding.GetCharCount(Byte[], Int32, Int32)

計算將起始於指定位元組指標的位元組序列解碼所產生的字元數。

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetCharCount (byte* bytes, int count);
C#
[System.CLSCompliant(false)]
public override int GetCharCount (byte* bytes, int count);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);

參數

bytes
Byte*

要解碼的第一個位元組指標。

count
Int32

要解碼的位元組數。

傳回

解碼指定位元組序列所產生的字元數。

屬性

例外狀況

bytesnull

count 小於零。

-或-

所產生的位元組數目大於可用整數傳回的數目上限。

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

備註

若要計算儲存所產生字元所需的 GetChars 確切數位大小,應用程式會使用 GetCharCount。 若要計算數位大小上限,應用程式應該使用 GetMaxCharCount。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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
.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 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCharCount(Byte[], Int32, Int32)

來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs

計算將指定位元組陣列中的位元組序列解碼所產生的字元數。

C#
public override int GetCharCount (byte[] bytes, int index, int count);

參數

bytes
Byte[]

包含要解碼之位元組序列的位元組陣列。

index
Int32

要解碼的第一個位元組索引。

count
Int32

要解碼的位元組數。

傳回

解碼指定位元組序列所產生的字元數。

例外狀況

bytesnull

indexcount 小於零。

-或-

indexcount 不代表 bytes 中有效的範圍。

-或-

所產生的位元組數目大於可用整數傳回的數目上限。

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例示範如何使用 GetCharCount 方法傳回譯位元組陣列中元素範圍所產生的字元數。

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();
        int charCount = ascii.GetCharCount(bytes, 6, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}

備註

若要計算儲存所產生字元所需的 GetChars 確切數位大小,應用程式會使用 GetCharCount。 若要計算數位大小上限,應用程式應該使用 GetMaxCharCount。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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
.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.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCharCount(ReadOnlySpan<Byte>)

來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs

計算解碼指定位元組範圍所產生的字元數目。

C#
public override int GetCharCount (ReadOnlySpan<byte> bytes);

參數

bytes
ReadOnlySpan<Byte>

包含要解碼之位元組集合的範圍。

傳回

解碼指定位元組範圍所產生的字元數目。

備註

若要計算儲存所產生字元所需的 GetChars 確切大小,請使用 GetCharCount。 若要計算大小上限,請使用 GetMaxCharCount。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9