本文整理汇总了C#中System.Text.UTF32Encoding.GetPreamble方法的典型用法代码示例。如果您正苦于以下问题:C# UTF32Encoding.GetPreamble方法的具体用法?C# UTF32Encoding.GetPreamble怎么用?C# UTF32Encoding.GetPreamble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.UTF32Encoding
的用法示例。
在下文中一共展示了UTF32Encoding.GetPreamble方法的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.IO;
using System.Text;
public class Example
{
public static void Main()
{
String s = "This is a string to write to a file using UTF-32 encoding.";
// Write a file using the default constructor without a BOM.
var enc = new UTF32Encoding(! BitConverter.IsLittleEndian, false);
Byte[] bytes = enc.GetBytes(s);
WriteToFile(@".\NoPreamble.txt", enc, bytes);
// Use BOM.
enc = new UTF32Encoding(! BitConverter.IsLittleEndian, true);
WriteToFile(@".\Preamble.txt", enc, bytes);
}
private static void WriteToFile(String fn, Encoding enc, Byte[] bytes)
{
var fs = new FileStream(fn, FileMode.Create);
Byte[] preamble = enc.GetPreamble();
fs.Write(preamble, 0, preamble.Length);
Console.WriteLine("Preamble has {0} bytes", preamble.Length);
fs.Write(bytes, 0, bytes.Length);
Console.WriteLine("Wrote {0} bytes to {1}.", fs.Length, fn);
fs.Close();
Console.WriteLine();
}
}
输出:
Preamble has 0 bytes Wrote 232 bytes to .\NoPreamble.txt. Preamble has 4 bytes Wrote 236 bytes to .\Preamble.txt.