本文整理匯總了C#中System.Text.Encoding.GetMaxCharCount方法的典型用法代碼示例。如果您正苦於以下問題:C# Encoding.GetMaxCharCount方法的具體用法?C# Encoding.GetMaxCharCount怎麽用?C# Encoding.GetMaxCharCount使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetMaxCharCount方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Encoding u32LE = Encoding.GetEncoding( "utf-32" );
Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );
// Use a 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 = "za\u0306\u01FD\u03B2";
// Encode the string using the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );
// Encode the string using 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 );
}
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 = enc.GetChars( bytes );
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 : 5 12 :za??ß
LE array with LE encoding : System.Text.UTF32Encoding : 5 12 :za??ß
*/