本文整理匯總了VB.NET中System.Text.UnicodeEncoding.GetPreamble方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET UnicodeEncoding.GetPreamble方法的具體用法?VB.NET UnicodeEncoding.GetPreamble怎麽用?VB.NET UnicodeEncoding.GetPreamble使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.UnicodeEncoding
的用法示例。
在下文中一共展示了UnicodeEncoding.GetPreamble方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: UnicodeEncodingExample
' 導入命名空間
Imports System.Text
Class UnicodeEncodingExample
Public Shared Sub Main()
Dim byteOrderMark() As Byte
Dim b As Byte
byteOrderMark = Encoding.Unicode.GetPreamble()
Console.WriteLine("Default (little-endian) Unicode Preamble:")
For Each b In byteOrderMark
Console.Write("[{0}]", b)
Next b
Console.WriteLine(ControlChars.NewLine)
Dim bigEndianUnicode As New UnicodeEncoding(True, True)
byteOrderMark = bigEndianUnicode.GetPreamble()
Console.WriteLine("Big-endian Unicode Preamble:")
For Each b In byteOrderMark
Console.Write("[{0}]", b)
Next b
End Sub
End Class
示例2: Example
' 導入命名空間
Imports System.IO
Imports System.Text
Module Example
Public Sub Main()
Dim s As String = "This is a string to write to a file using UTF-16 encoding."
' Write a file using the default constructor without a BOM.
Dim enc As New UnicodeEncoding(Not BitConverter.IsLittleEndian, False)
Dim bytes() As Byte = enc.GetBytes(s)
WriteToFile("NoPreamble.txt", enc, bytes)
' Use BOM.
enc = New UnicodeEncoding(Not BitConverter.IsLittleEndian, True)
WriteToFile("Preamble.txt", enc, bytes)
End Sub
Private Sub WriteToFile(fn As String, enc As Encoding, bytes As Byte())
Dim fs As New FileStream(fn, FileMode.Create)
Dim preamble() As Byte = 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()
End Sub
End Module
輸出:
Preamble has 0 bytes Wrote 116 bytes to .\NoPreamble.txt. Preamble has 2 bytes Wrote 118 bytes to .\Preamble.txt.