微软在VS.net2005中才加入了处理32位编码的Unicode字符的类库,
.NET之前的版本我没见有对它的支持。
//*****************************************************************************
UTF32Encoding Class
[此帮助主题属于 Visual Studio 预发布版的文档,以后正式发布时可能会有改动。空的帮助主题作为占位符包含在内。]
Note: This class is new in the .NET Framework version 2.0.
Represents a UTF-32 encoding of Unicode characters.
Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
//*****************************************************************************
using System;
using System.Text;
public class SamplesUTF32Encoding {
public static void Main() {
// Create an instance of UTF32Encoding using little-endian byte order.
// This will be used for encoding.
UTF32Encoding u32LE = new UTF32Encoding( false, true );
// Create two instances of UTF32Encoding using big-endian byte order: one with error detection and one without.
// These will be used for decoding.
UTF32Encoding u32withED = new UTF32Encoding( true, true, true );
UTF32Encoding u32noED = new UTF32Encoding( true, true, false );
// 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";
// Encode the string using little-endian byte order.
byte[] myBytes = new byte[u32LE.GetByteCount( myStr )];
u32LE.GetBytes( myStr, 0, myStr.Length, myBytes, 0 );
// Decode the byte array with error detection.
Console.WriteLine( "Decoding with error detection:" );
PrintDecodedString( myBytes, u32withED );
// Decode the byte array without error detection.
Console.WriteLine( "Decoding without error detection:" );
PrintDecodedString( myBytes, u32noED );
}
// Decode the bytes and display the string.
public static void PrintDecodedString( byte[] bytes, Encoding enc ) {
try {
Console.WriteLine( " Decoded string: {0}", enc.GetString( bytes, 0, bytes.Length ) );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.ToString() );
}
Console.WriteLine();
}
}