UnicodeEncoding.GetCharCount 方法

定義

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

多載

GetCharCount(Byte*, Int32)

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

GetCharCount(Byte[], Int32, Int32)

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

GetCharCount(Byte*, Int32)

重要

此 API 不符合 CLS 規範。

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

public:
 override int GetCharCount(System::Byte* bytes, int count);
[System.CLSCompliant(false)]
public override int GetCharCount (byte* bytes, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
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)>]
override this.GetCharCount : nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
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

要解碼的位元組數。

傳回

Int32

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

屬性

例外狀況

bytesnull (Nothing)。

count 小於零。

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

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

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback

備註

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

使用錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。

另請參閱

適用於

GetCharCount(Byte[], Int32, Int32)

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

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

要解碼的位元組數。

傳回

Int32

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

例外狀況

bytesnull (Nothing)。

indexcount 小於零。

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

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

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

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback

範例

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

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {85,0,110,0,105,0,99,0,111,0,100,0,101,0};
   UnicodeEncoding^ Unicode = gcnew UnicodeEncoding;
   int charCount = Unicode->GetCharCount( bytes, 2, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

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

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte = {85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0}
        
        Dim uni As New UnicodeEncoding()
        Dim charCount As Integer = uni.GetCharCount(bytes, 2, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

備註

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

使用錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。

另請參閱

適用於