本文整理汇总了VB.NET中System.Text.UTF8Encoding.GetPreamble方法的典型用法代码示例。如果您正苦于以下问题:VB.NET UTF8Encoding.GetPreamble方法的具体用法?VB.NET UTF8Encoding.GetPreamble怎么用?VB.NET UTF8Encoding.GetPreamble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.UTF8Encoding
的用法示例。
在下文中一共展示了UTF8Encoding.GetPreamble方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Text
Module Example
Public Sub Main()
' The default constructor does not provide a preamble.
Dim UTF8NoPreamble As New UTF8Encoding()
Dim UTF8WithPreamble As New UTF8Encoding(True)
Dim preamble() As Byte
preamble = UTF8NoPreamble.GetPreamble()
Console.WriteLine("UTF8NoPreamble")
Console.WriteLine(" preamble length: {0}", preamble.Length)
Console.Write(" preamble: ")
ShowArray(preamble)
Console.WriteLine()
preamble = UTF8WithPreamble.GetPreamble()
Console.WriteLine("UTF8WithPreamble")
Console.WriteLine(" preamble length: {0}", preamble.Length)
Console.Write(" preamble: ")
ShowArray(preamble)
End Sub
Public Sub ShowArray(bytes As Byte())
For Each b In bytes
Console.Write("{0:X2} ", b)
Next
Console.WriteLine()
End Sub
End Module
输出:
UTF8NoPreamble preamble length: 0 preamble: UTF8WithPreamble preamble length: 3 preamble: EF BB BF
示例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-8 encoding."
' Write a file using the default constructor without a BOM.
Dim enc As New UTF8Encoding()
Dim bytes() As Byte = enc.GetBytes(s)
WriteToFile("NoPreamble.txt", enc, bytes)
' Use BOM.
enc = New UTF8Encoding(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 57 bytes to NoPreamble.txt. Preamble has 3 bytes Wrote 60 bytes to Preamble.txt.