UTF8Encoding 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 UTF8Encoding 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| UTF8Encoding() |
初始化 UTF8Encoding 類別的新執行個體。 |
| UTF8Encoding(Boolean) |
初始化 UTF8Encoding 類別的新執行個體。 一個參數指定是否提供 Unicode 位元組序標記。 |
| UTF8Encoding(Boolean, Boolean) |
初始化 UTF8Encoding 類別的新執行個體。 參數會指定是否提供 Unicode 位元組順序標記,以及當偵測到無效編碼時是否拋出例外。 |
UTF8Encoding()
初始化 UTF8Encoding 類別的新執行個體。
public:
UTF8Encoding();
public UTF8Encoding();
Public Sub New ()
範例
以下範例會建立一個新 UTF8Encoding 實例並顯示其名稱。
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
UTF8Encoding utf8 = new UTF8Encoding();
String encodingName = utf8.EncodingName;
Console.WriteLine("Encoding name: " + encodingName);
}
}
Imports System.Text
Class UTF8EncodingExample
Public Shared Sub Main()
Dim utf8 As New UTF8Encoding()
Dim encodingName As String = utf8.EncodingName
Console.WriteLine("Encoding name: " & encodingName)
End Sub
End Class
備註
此建構子建立一個不提供 Unicode 位元組順序標記的實例,且在偵測到無效編碼時不會拋出例外。
Caution
出於安全考量,我們建議您呼叫一個帶有 throwOnInvalidBytes 參數的建構子並將其值 true設為 ,來啟用錯誤偵測。
另請參閱
適用於
UTF8Encoding(Boolean)
初始化 UTF8Encoding 類別的新執行個體。 一個參數指定是否提供 Unicode 位元組序標記。
public:
UTF8Encoding(bool encoderShouldEmitUTF8Identifier);
public UTF8Encoding(bool encoderShouldEmitUTF8Identifier);
new System.Text.UTF8Encoding : bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean)
參數
- encoderShouldEmitUTF8Identifier
- Boolean
true 指定該方法回 GetPreamble() 傳 Unicode 位元組序標記;否則, false。
範例
以下範例建立一個新 UTF8Encoding 實例,並指定該方法應 GetPreamble 輸出 Unicode 位元組順序標記前綴。 接著,該 GetPreamble 方法會回傳 Unicode 位元組順序標記的前綴。
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
UTF8Encoding utf8 = new UTF8Encoding();
UTF8Encoding utf8EmitBOM = new UTF8Encoding(true);
Console.WriteLine("utf8 preamble:");
ShowArray(utf8.GetPreamble());
Console.WriteLine("utf8EmitBOM:");
ShowArray(utf8EmitBOM.GetPreamble());
}
public static void ShowArray(Array theArray) {
foreach (Object o in theArray) {
Console.Write("[{0}]", o);
}
Console.WriteLine();
}
}
Imports System.Text
Class UTF8EncodingExample
Public Shared Sub Main()
Dim utf8 As New UTF8Encoding()
Dim utf8EmitBOM As New UTF8Encoding(True)
Console.WriteLine("utf8 preamble:")
ShowArray(utf8.GetPreamble())
Console.WriteLine("utf8EmitBOM:")
ShowArray(utf8EmitBOM.GetPreamble())
End Sub
Public Shared Sub ShowArray(theArray As Array)
Dim o As Object
For Each o In theArray
Console.Write("[{0}]", o)
Next o
Console.WriteLine()
End Sub
End Class
備註
此建構子建立一個實例,當偵測到無效編碼時不會拋出例外。
Caution
出於安全考量,你應該呼叫包含 throwOnInvalidBytes 參數的建構子並將其值 true設為 來啟用錯誤偵測。
參數 encoderShouldEmitUTF8Identifier 控制方法的 GetPreamble 運作。 若 true,該方法會回傳包含 Unicode 位元組順序標記(BOM)的位元組陣列,格式為 UTF-8。 若 false,則回傳一個零長度的位元組陣列。 然而,設定 encoderShouldEmitUTF8Identifier 為 true 並不會使 GetBytes 方法在位元組陣列開頭加上 BOM,也不會將 GetByteCount BOM 中的位元組數納入位元組計數。
另請參閱
適用於
UTF8Encoding(Boolean, Boolean)
初始化 UTF8Encoding 類別的新執行個體。 參數會指定是否提供 Unicode 位元組順序標記,以及當偵測到無效編碼時是否拋出例外。
public:
UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
public UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
new System.Text.UTF8Encoding : bool * bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean, throwOnInvalidBytes As Boolean)
參數
- encoderShouldEmitUTF8Identifier
- Boolean
true指定GetPreamble()該方法應回傳 Unicode 位元組順序標記;否則,。 false
- throwOnInvalidBytes
- Boolean
true當偵測到無效編碼時拋出例外;否則,。 false
範例
以下範例建立一個新 UTF8Encoding 實例,指定 GetPreamble 該方法不應發出 Unicode 位元組順序標記前綴,且當偵測到無效編碼時應拋出例外。 此建構子的行為會與預設 UTF8Encoding() 建構子比較,後者在偵測到無效編碼時不會拋出例外。 這兩個 UTF8Encoding 實例編碼了一個字元陣列,該陣列中包含兩個高代詞(U+D801 和 U+D802),這是無效的字元序列;高代詞後應總是緊接著低代詞。
using System;
using System.Text;
class Example
{
public static void Main()
{
UTF8Encoding utf8 = new UTF8Encoding();
UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);
// Create an array with two high surrogates in a row (\uD801, \uD802).
Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'};
// The following method call will not throw an exception.
Byte[] bytes = utf8.GetBytes(chars);
ShowArray(bytes);
Console.WriteLine();
try {
// The following method call will throw an exception.
bytes = utf8ThrowException.GetBytes(chars);
ShowArray(bytes);
}
catch (EncoderFallbackException e) {
Console.WriteLine("{0} exception\nMessage:\n{1}",
e.GetType().Name, e.Message);
}
}
public static void ShowArray(Array theArray) {
foreach (Object o in theArray)
Console.Write("{0:X2} ", o);
Console.WriteLine();
}
}
// The example displays the following output:
// 61 62 63 EF BF BD EF BF BD 64
//
// EncoderFallbackException exception
// Message:
// Unable to translate Unicode character \uD801 at index 3 to specified code page.
Imports System.Text
Class Example
Public Shared Sub Main()
Dim utf8 As New UTF8Encoding()
Dim utf8ThrowException As New UTF8Encoding(False, True)
' Create an array with two high surrogates in a row (\uD801, \uD802).
Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(&hD801), ChrW(&hD802), "d"c}
' The following method call will not throw an exception.
Dim bytes As Byte() = utf8.GetBytes(chars)
ShowArray(bytes)
Console.WriteLine()
Try
' The following method call will throw an exception.
bytes = utf8ThrowException.GetBytes(chars)
ShowArray(bytes)
Catch e As EncoderFallbackException
Console.WriteLine("{0} exception{2}Message:{2}{1}",
e.GetType().Name, e.Message, vbCrLf)
End Try
End Sub
Public Shared Sub ShowArray(theArray As Array)
For Each o In theArray
Console.Write("{0:X2} ", o)
Next
Console.WriteLine()
End Sub
End Class
' The example displays the following output:
' 61 62 63 EF BF BD EF BF BD 64
'
' EncoderFallbackException exception
' Message:
' Unable to translate Unicode character \uD801 at index 3 to specified code page.
備註
參數 encoderShouldEmitUTF8Identifier 控制方法的 GetPreamble 運作。 若 true,該方法會回傳包含 Unicode 位元組順序標記(BOM)的位元組陣列,格式為 UTF-8。 若 false,則回傳一個零長度的位元組陣列。 然而,設定 encoderShouldEmitUTF8Identifier 為 true 並不會使 GetBytes 方法在位元組陣列開頭加上 BOM,也不會將 GetByteCount BOM 中的位元組數納入位元組計數。
若 throwOnInvalidBytes , true偵測無效位元組序列的方法會 System.ArgumentException 拋出例外。 否則,方法不會拋出例外,且無效序列會被忽略。
Caution
出於安全考量,你應該透過呼叫包含 throwOnInvalidBytes 參數的建構子並將該參數設為 true來啟用錯誤偵測。