本文整理汇总了C#中System.Text.UTF32Encoding.UTF32Encoding构造函数的典型用法代码示例。如果您正苦于以下问题:C# UTF32Encoding构造函数的具体用法?C# UTF32Encoding怎么用?C# UTF32Encoding使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Text.UTF32Encoding
的用法示例。
在下文中一共展示了UTF32Encoding构造函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Text;
public class SamplesUTF32Encoding
{
public static void Main()
{
// Create instances of UTF32Encoding, with the byte order mark and without.
UTF32Encoding u32LeNone = new UTF32Encoding();
UTF32Encoding u32BeNone = new UTF32Encoding( true, false );
UTF32Encoding u32LeBom = new UTF32Encoding( false, true );
UTF32Encoding u32BeBom = new UTF32Encoding( true, true );
// Display the preamble for each instance.
PrintHexBytes( u32LeNone.GetPreamble() );
PrintHexBytes( u32BeNone.GetPreamble() );
PrintHexBytes( u32LeBom.GetPreamble() );
PrintHexBytes( u32BeBom.GetPreamble() );
}
public static void PrintHexBytes( byte[] bytes )
{
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
输出:
FF FE 00 00FF FE 00 00 00 00 FE FF
示例2: Main
//引入命名空间
using System;
using System.Text;
public class Example
{
public static void Main()
{
// Create a UTF32Encoding object with error detection enabled.
var encExc = new UTF32Encoding(! BitConverter.IsLittleEndian, true, true);
// Create a UTF32Encoding object with error detection disabled.
var encRepl = new UTF32Encoding(! BitConverter.IsLittleEndian, true, false);
// Create a byte arrays from a string, and add an invalid surrogate pair, as follows.
// 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)
// an invalid low surrogate (U+01FF)
String s = "za\u0306\u01FD\u03B2";
// Encode the string using little-endian byte order.
int index = encExc.GetByteCount(s);
Byte[] bytes = new Byte[index + 4];
encExc.GetBytes(s, 0, s.Length, bytes, 0);
bytes[index] = 0xFF;
bytes[index + 1] = 0xD8;
bytes[index + 2] = 0xFF;
bytes[index + 3] = 0x01;
// Decode the byte array with error detection.
Console.WriteLine("Decoding with error detection:");
PrintDecodedString(bytes, encExc);
// Decode the byte array without error detection.
Console.WriteLine("Decoding without error detection:");
PrintDecodedString(bytes, encRepl);
}
// 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 (DecoderFallbackException e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine();
}
}
输出:
Decoding with error detection: System.Text.DecoderFallbackException: Unable to translate bytes [FF][D8][FF][01] at index 20 from specified code page to Unicode. at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index) at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index ) at System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes) at System.Text.UTF32Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDeco der) at System.Text.UTF32Encoding.GetString(Byte[] bytes, Int32 index, Int32 count) at Example.PrintDecodedString(Byte[] bytes, Encoding enc) Decoding without error detection: Decoded string: zăǽβ�