UTF8Encoding 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 UTF8Encoding 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| UTF8Encoding() |
初始化 UTF8Encoding 类的新实例。 |
| UTF8Encoding(Boolean) |
初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记。 |
| UTF8Encoding(Boolean, Boolean) |
初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记,以及是否在检测到无效编码时引发异常。 |
UTF8Encoding()
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 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
出于安全原因,我们建议通过调用具有参数的构造函数并将其值设置为 <
另请参阅
适用于
UTF8Encoding(Boolean)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 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
示例
以下示例创建一个新 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,该方法返回一个字节数组,其中包含 UTF-8 格式的 Unicode 字节顺序标记(BOM)。 如果 false返回零长度字节数组。 但是,设置为encoderShouldEmitUTF8Identifiertrue不会导致GetBytes方法在字节数组的开头为 BOM 添加前缀,也不会导致GetByteCount该方法在字节计数中包含 BOM 中的字节数。
另请参阅
适用于
UTF8Encoding(Boolean, Boolean)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 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
- 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,该方法返回一个字节数组,其中包含 UTF-8 格式的 Unicode 字节顺序标记(BOM)。 如果 false返回零长度字节数组。 但是,设置为encoderShouldEmitUTF8Identifiertrue不会导致GetBytes方法在字节数组的开头为 BOM 添加前缀,也不会导致GetByteCount该方法在字节计数中包含 BOM 中的字节数。
throwOnInvalidBytes如果是true,则检测无效字节序列的方法将引发System.ArgumentException异常。 否则,该方法不会引发异常,并且忽略无效序列。
Caution
出于安全原因,应通过调用包含 throwOnInvalidBytes 参数和将该参数设置为的构造函数来 true启用错误检测。