本文整理匯總了C#中System.Text.UnicodeEncoding.GetPreamble方法的典型用法代碼示例。如果您正苦於以下問題:C# UnicodeEncoding.GetPreamble方法的具體用法?C# UnicodeEncoding.GetPreamble怎麽用?C# UnicodeEncoding.GetPreamble使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.UnicodeEncoding
的用法示例。
在下文中一共展示了UnicodeEncoding.GetPreamble方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
Byte[] byteOrderMark;
byteOrderMark = Encoding.Unicode.GetPreamble();
Console.WriteLine("Default (little-endian) Unicode Preamble:");
foreach (Byte b in byteOrderMark) {
Console.Write("[{0}]", b);
}
Console.WriteLine("\n");
UnicodeEncoding bigEndianUnicode = new UnicodeEncoding(true, true);
byteOrderMark = bigEndianUnicode.GetPreamble();
Console.WriteLine("Big-endian Unicode Preamble:");
foreach (Byte b in byteOrderMark) {
Console.Write("[{0}]", b);
}
}
}
示例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-16 encoding.";
// Write a file using a Unicode encoding object without a BOM.
var enc = new UnicodeEncoding(! BitConverter.IsLittleEndian, false);
Byte[] bytes = enc.GetBytes(s);
WriteToFile(@".\NoPreamble.txt", enc, bytes);
// Use BOM.
enc = new UnicodeEncoding(! 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 116 bytes to .\NoPreamble.txt. Preamble has 2 bytes Wrote 118 bytes to .\Preamble.txt.