UTF8Encoding.GetCharCount 方法

定義

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

多載

GetCharCount(Byte[], Int32, Int32)

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

GetCharCount(Byte*, Int32)

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

GetCharCount(ReadOnlySpan<Byte>)

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

GetCharCount(Byte[], Int32, Int32)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs

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

public:
 override int GetCharCount(cli::array <System::Byte> ^ bytes, int index, int count);
public override int GetCharCount (byte[] bytes, int index, int count);
override this.GetCharCount : byte[] * int * int -> int
Public Overrides Function GetCharCount (bytes As Byte(), index As Integer, count As Integer) As Integer

參數

bytes
Byte[]

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

index
Int32

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

count
Int32

要解碼的位元組數。

傳回

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

例外狀況

bytesnull

indexcount 小於零。

-或-

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

-或-

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

已啟用錯誤偵測,而且 bytes 包含無效的位元組序列。

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例會 GetCharCount 使用 方法來傳回譯位元組陣列中專案範圍所產生的字元數。

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {85,84,70,56,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int charCount = utf8->GetCharCount( bytes, 2, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();
        int charCount = utf8.GetCharCount(bytes, 2, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

備註

若要計算儲存結果字元所需的 GetChars 確切陣列大小,請呼叫 GetCharCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxCharCount 方法。 方法 GetCharCount 通常會配置較少的記憶體,而 GetMaxCharCount 方法通常會更快執行。

發生錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 例外狀況。 若未偵測錯誤,則會忽略不正確序列,而且不會擲回例外狀況。

另請參閱

適用於

GetCharCount(Byte*, Int32)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs

重要

此 API 不符合 CLS 規範。

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

public:
 override int GetCharCount(System::Byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetCharCount (byte* bytes, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetCharCount : nativeptr<byte> * int -> int

參數

bytes
Byte*

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

count
Int32

要解碼的位元組數。

傳回

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

屬性

例外狀況

bytesnull

count 小於零。

-或-

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

已啟用錯誤偵測,而且 bytes 包含無效的位元組序列。

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

備註

若要計算儲存結果字元所需的 GetChars 確切陣列大小,請呼叫 GetCharCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxCharCount 方法。 方法 GetCharCount 通常會配置較少的記憶體,而 GetMaxCharCount 方法通常會更快執行。

發生錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 例外狀況。 若未偵測錯誤,則會忽略不正確序列,而且不會擲回例外狀況。

另請參閱

適用於

GetCharCount(ReadOnlySpan<Byte>)

來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs
來源:
UTF8Encoding.cs

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

public:
 override int GetCharCount(ReadOnlySpan<System::Byte> bytes);
public override int GetCharCount (ReadOnlySpan<byte> bytes);
override this.GetCharCount : ReadOnlySpan<byte> -> int
Public Overrides Function GetCharCount (bytes As ReadOnlySpan(Of Byte)) As Integer

參數

bytes
ReadOnlySpan<Byte>

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

傳回

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

備註

若要計算儲存結果字元所需的 GetChars 確切大小,請呼叫 GetCharCount 方法。 若要計算大小上限,請呼叫 GetMaxCharCount 方法。 方法 GetCharCount 通常會配置較少的記憶體,而 GetMaxCharCount 方法通常會更快執行。

發生錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 例外狀況。 若未偵測錯誤,則會忽略不正確序列,而且不會擲回例外狀況。

適用於