ASCIIEncoding.GetMaxCharCount(Int32) 方法

定義

計算將指定數目的位元組解碼所產生的最大字元數。

public:
 override int GetMaxCharCount(int byteCount);
public override int GetMaxCharCount (int byteCount);
override this.GetMaxCharCount : int -> int
Public Overrides Function GetMaxCharCount (byteCount As Integer) As Integer

參數

byteCount
Int32

要解碼的位元組數。

傳回

Int32

解碼指定位元組數所產生的最大字元數。

例外狀況

byteCount 小於零。

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

範例

下列範例示範如何使用 GetMaxCharCount 方法來計算解碼指定位元組數目所需的字元數上限。

using namespace System;
using namespace System::Text;
int main()
{
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int byteCount = 8;
   int maxCharCount = ascii->GetMaxCharCount( byteCount );
   Console::WriteLine( "Maximum of {0} characters needed to decode {1} bytes.", maxCharCount, byteCount );
}
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        ASCIIEncoding ascii = new ASCIIEncoding();
        int byteCount = 8;
        int maxCharCount = ascii.GetMaxCharCount(byteCount);
        Console.WriteLine(
            "Maximum of {0} characters needed to decode {1} bytes.",
            maxCharCount,
            byteCount
        );
    }
}
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim ascii As New ASCIIEncoding()
        Dim byteCount As Integer = 8
        Dim maxCharCount As Integer = ascii.GetMaxCharCount(byteCount)
        Console.WriteLine( _
            "Maximum of {0} characters needed to decode {1} bytes.", _
            maxCharCount, _
            byteCount _
        )
    End Sub
End Class

備註

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

GetMaxCharCount 會擷取最差大小寫的數位,包括目前選取 DecoderFallback 的最差大小寫。 如果解碼器後援存在,其後援長度上限為 n,方法會 GetMaxCharCount 傳回 n * byteCount

GetMaxCharCount 與 沒有關聯 GetBytes 。 如果您的應用程式需要與 搭配 GetBytes 使用的類似函式,它應該使用 GetMaxByteCount

注意

GetMaxCharCount(N) 不一定與 相同的值 N* GetMaxCharCount(1)

適用於

另請參閱