UTF32Encoding.GetMaxCharCount(Int32) 方法
定义
计算对指定数目的字节进行解码时产生的最大字符数。Calculates the maximum number of characters produced by decoding the specified number of bytes.
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
要解码的字节数。The number of bytes to decode.
返回
对指定数目的字节进行解码时所产生的最大字符数。The maximum number of characters produced by decoding the specified number of bytes.
例外
byteCount 小于零。byteCount is less than zero.
或-or- 得到的字节数大于可作为整数返回的最大数量。The resulting number of bytes is greater than the maximum number that can be returned as an integer.
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)A fallback occurred (for more information, see Character Encoding in .NET) -和--and- 将 DecoderFallback 设置为 DecoderExceptionFallback。DecoderFallback is set to DecoderExceptionFallback.
示例
下面的示例将字符串编码为一个字节数组,然后将这些字节解码为一个字符数组。The following example encodes a string into an array of bytes, and then decodes the bytes into an array of characters.
using namespace System;
using namespace System::Text;
void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc );
int main()
{
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
UTF32Encoding^ u32LE = gcnew UTF32Encoding( false,true,true );
UTF32Encoding^ u32BE = gcnew UTF32Encoding( true,true,true );
// Create byte arrays from the same string containing the following characters:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
String^ myStr = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
// barrBE uses the big-endian byte order.
array<Byte>^barrBE = gcnew array<Byte>(u32BE->GetByteCount( myStr ));
u32BE->GetBytes( myStr, 0, myStr->Length, barrBE, 0 );
// barrLE uses the little-endian byte order.
array<Byte>^barrLE = gcnew array<Byte>(u32LE->GetByteCount( myStr ));
u32LE->GetBytes( myStr, 0, myStr->Length, barrLE, 0 );
// Get the char counts and decode the byte arrays.
Console::Write( "BE array with BE encoding : " );
PrintCountsAndChars( barrBE, u32BE );
Console::Write( "LE array with LE encoding : " );
PrintCountsAndChars( barrLE, u32LE );
// Decode the byte arrays using an encoding with a different byte order.
Console::Write( "BE array with LE encoding : " );
try
{
PrintCountsAndChars( barrBE, u32LE );
}
catch ( System::ArgumentException^ e )
{
Console::WriteLine( e->Message );
}
Console::Write( "LE array with BE encoding : " );
try
{
PrintCountsAndChars( barrLE, u32BE );
}
catch ( System::ArgumentException^ e )
{
Console::WriteLine( e->Message );
}
}
void PrintCountsAndChars( array<Byte>^bytes, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-25} :", enc );
// Display the exact character count.
int iCC = enc->GetCharCount( bytes );
Console::Write( " {0,-3}", iCC );
// Display the maximum character count.
int iMCC = enc->GetMaxCharCount( bytes->Length );
Console::Write( " {0,-3} :", iMCC );
// Decode the bytes and display the characters.
array<Char>^chars = gcnew array<Char>(iCC);
enc->GetChars( bytes, 0, bytes->Length, chars, 0 );
Console::WriteLine( chars );
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
BE array with BE encoding : System.Text.UTF32Encoding : 7 14 :za??�?
LE array with LE encoding : System.Text.UTF32Encoding : 7 14 :za??�?
BE array with LE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.
LE array with BE encoding : System.Text.UTF32Encoding :Invalid byte was found at byte index 3.
*/
using System;
using System.Text;
public class SamplesUTF32Encoding {
public static void Main() {
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
UTF32Encoding u32LE = new UTF32Encoding( false, true, true );
UTF32Encoding u32BE = new UTF32Encoding( true, true, true );
// Create byte arrays from the same string containing the following characters:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
// barrBE uses the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );
// barrLE uses the little-endian byte order.
byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );
// Get the char counts and decode the byte arrays.
Console.Write( "BE array with BE encoding : " );
PrintCountsAndChars( barrBE, u32BE );
Console.Write( "LE array with LE encoding : " );
PrintCountsAndChars( barrLE, u32LE );
// Decode the byte arrays using an encoding with a different byte order.
Console.Write( "BE array with LE encoding : " );
try {
PrintCountsAndChars( barrBE, u32LE );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.Message );
}
Console.Write( "LE array with BE encoding : " );
try {
PrintCountsAndChars( barrLE, u32BE );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.Message );
}
}
public static void PrintCountsAndChars( byte[] bytes, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-25} :", enc.ToString() );
// Display the exact character count.
int iCC = enc.GetCharCount( bytes );
Console.Write( " {0,-3}", iCC );
// Display the maximum character count.
int iMCC = enc.GetMaxCharCount( bytes.Length );
Console.Write( " {0,-3} :", iMCC );
// Decode the bytes and display the characters.
char[] chars = new char[iCC];
enc.GetChars( bytes, 0, bytes.Length, chars, 0 );
Console.WriteLine( chars );
}
}
Imports System.Text
Public Class SamplesUTF32Encoding
Public Shared Sub Main()
' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Dim u32LE As New UTF32Encoding(False, True, True)
Dim u32BE As New UTF32Encoding(True, True, True)
' Create byte arrays from the same string containing the following characters:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' barrBE uses the big-endian byte order.
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)
' barrLE uses the little-endian byte order.
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)
' Get the char counts and decode the byte arrays.
Console.Write("BE array with BE encoding : ")
PrintCountsAndChars(barrBE, u32BE)
Console.Write("LE array with LE encoding : ")
PrintCountsAndChars(barrLE, u32LE)
' Decode the byte arrays using an encoding with a different byte order.
Console.Write("BE array with LE encoding : ")
Try
PrintCountsAndChars(barrBE, u32LE)
Catch e As System.ArgumentException
Console.WriteLine(e.Message)
End Try
Console.Write("LE array with BE encoding : ")
Try
PrintCountsAndChars(barrLE, u32BE)
Catch e As System.ArgumentException
Console.WriteLine(e.Message)
End Try
End Sub
Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-25} :", enc.ToString())
' Display the exact character count.
Dim iCC As Integer = enc.GetCharCount(bytes)
Console.Write(" {0,-3}", iCC)
' Display the maximum character count.
Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
Console.Write(" {0,-3} :", iMCC)
' Decode the bytes and display the characters.
Dim chars(iCC) As Char
enc.GetChars(bytes, 0, bytes.Length, chars, 0)
Console.WriteLine(chars)
End Sub
End Class
注解
若要计算存储所生成的字符所需的确切数组大小 GetChars ,请调用 GetCharCount 方法。To calculate the exact array size required by GetChars to store the resulting characters, you call the GetCharCount method. 若要计算最大数组大小,请调用 GetMaxCharCount 方法。To calculate the maximum array size, you call the GetMaxCharCount method. GetCharCount方法通常分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.
GetMaxCharCount 是最差的事例数,包括当前所选的最糟糕的情况 DecoderFallback 。GetMaxCharCount is a worst-case number, including the worst case for the currently selected DecoderFallback. 如果使用可能大的字符串选择回退,则 GetMaxCharCount 可以返回大值。If a fallback is chosen with a potentially large string, GetMaxCharCount can return large values.
在大多数情况下,此方法为小字符串返回合理的数字。In most cases, this method returns reasonable numbers for small strings. 对于大字符串,可能需要在超过更合理缓冲区的罕见情况下,在使用非常大的缓冲区和捕获错误之间进行选择。For large strings, you might have to choose between using very large buffers and catching errors in the rare case that a more reasonable buffer is exceeded. 你可能还需要考虑使用不同的方法,并使用 GetCharCount 或 Convert 。You might also want to consider a different approach and use GetCharCount or Convert.
GetMaxCharCount 与之间没有关系 GetBytes 。GetMaxCharCount has no relationship to GetBytes. 如果需要与一起使用的类似功能 GetBytes ,请使用 GetMaxByteCount 。If you need a similar function to use with GetBytes, use GetMaxByteCount.
备注
GetMaxCharCount(N)不一定与相同 N* GetMaxCharCount(1) 。GetMaxCharCount(N) is not necessarily the same value as N* GetMaxCharCount(1).