UTF8Encoding.GetMaxCharCount(Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
計算將指定數目的位元組解碼所產生的最大字元數。
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
要解碼的位元組數。
傳回
解碼指定位元組數所產生的最大字元數。
例外狀況
發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback。
範例
下列範例會 GetMaxCharCount 使用 方法來傳回譯指定位元組數目所產生的最大字元數。
using namespace System;
using namespace System::Text;
int main()
{
UTF8Encoding^ utf8 = gcnew UTF8Encoding;
int byteCount = 8;
int maxCharCount = utf8->GetMaxCharCount( byteCount );
Console::WriteLine( "Maximum of {0} characters needed to decode {1} bytes.", maxCharCount, byteCount );
}
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
UTF8Encoding utf8 = new UTF8Encoding();
int byteCount = 8;
int maxCharCount = utf8.GetMaxCharCount(byteCount);
Console.WriteLine(
"Maximum of {0} characters needed to decode {1} bytes.",
maxCharCount,
byteCount
);
}
}
Imports System.Text
Class UTF8EncodingExample
Public Shared Sub Main()
Dim utf8 As New UTF8Encoding()
Dim byteCount As Integer = 8
Dim maxCharCount As Integer = utf8.GetMaxCharCount(byteCount)
Console.WriteLine( _
"Maximum of {0} characters needed to decode {1} bytes.", _
maxCharCount, _
byteCount _
)
End Sub
End Class
備註
若要計算儲存結果字元所需的 GetChars 確切陣列大小,請呼叫 GetCharCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxCharCount 方法。 方法 GetCharCount 通常會配置較少的記憶體,而 GetMaxCharCount 方法通常會更快執行。
GetMaxCharCount 是最差大小寫的數位,包括目前選取 DecoderFallback 的最差大小寫。 如果選擇具有可能大型字串的後援, GetMaxCharCount 可以傳回大值。
在大部分情況下,此方法會傳回小型字串的合理數位。 對於大型字串,您可能必須在使用非常大的緩衝區和攔截錯誤之間選擇,在罕見的情況下,超過更合理的緩衝區。 您可能也想要考慮使用 GetCharCount 或 Encoder.Convert 的不同方法。
GetMaxCharCount 與 沒有關聯 GetBytes 。 如果您的應用程式需要與 搭配 GetBytes 使用的類似函式,它應該使用 GetMaxByteCount 。
注意
GetMaxCharCount(N)
不一定與 N* GetMaxCharCount(1)
相同值。