UnicodeEncoding 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 UnicodeEncoding 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| UnicodeEncoding() |
初始化 UnicodeEncoding 类的新实例。 |
| UnicodeEncoding(Boolean, Boolean) |
初始化 UnicodeEncoding 类的新实例。 参数指定是否使用大字节字节顺序,以及该方法是否 GetPreamble() 返回 Unicode 字节顺序标记。 |
| UnicodeEncoding(Boolean, Boolean, Boolean) |
初始化 UnicodeEncoding 类的新实例。 参数指定是否使用大字节字节顺序、是否提供 Unicode 字节顺序标记,以及是否在检测到无效编码时引发异常。 |
UnicodeEncoding()
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
初始化 UnicodeEncoding 类的新实例。
public:
UnicodeEncoding();
public UnicodeEncoding();
Public Sub New ()
示例
以下示例演示如何创建新 UnicodeEncoding 实例并显示编码的名称。
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
UnicodeEncoding unicode = new UnicodeEncoding();
String encodingName = unicode.EncodingName;
Console.WriteLine("Encoding name: " + encodingName);
}
}
Imports System.Text
Class UnicodeEncodingExample
Public Shared Sub Main()
Dim uni As New UnicodeEncoding()
Dim encodingName As String = uni.EncodingName
Console.WriteLine("Encoding name: " & encodingName)
End Sub
End Class
注解
此构造函数创建一个实例,该实例使用小字节字节顺序,提供 Unicode 字节顺序标记,并在检测到无效编码时不引发异常。
Caution
出于安全原因,应通过调用
适用于
UnicodeEncoding(Boolean, Boolean)
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
初始化 UnicodeEncoding 类的新实例。 参数指定是否使用大字节字节顺序,以及该方法是否 GetPreamble() 返回 Unicode 字节顺序标记。
public:
UnicodeEncoding(bool bigEndian, bool byteOrderMark);
public UnicodeEncoding(bool bigEndian, bool byteOrderMark);
new System.Text.UnicodeEncoding : bool * bool -> System.Text.UnicodeEncoding
Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean)
参数
- bigEndian
- Boolean
true 使用大字节字节顺序(最重要字节第一),或使用 false 小字节字节顺序(最不重要的字节优先)。
- byteOrderMark
- Boolean
示例
下面的示例演示如何创建一个新 UnicodeEncoding 实例,该实例指定是支持小 endian 还是大字节字节排序以及 Unicode 字节顺序标记。
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
// Create a UnicodeEncoding without parameters.
UnicodeEncoding unicode = new UnicodeEncoding();
// Create a UnicodeEncoding to support little-endian byte ordering
// and include the Unicode byte order mark.
UnicodeEncoding unicodeLittleEndianBOM =
new UnicodeEncoding(false, true);
// Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicode.Equals(unicodeLittleEndianBOM));
// Create a UnicodeEncoding to support little-endian byte ordering
// and not include the Unicode byte order mark.
UnicodeEncoding unicodeLittleEndianNoBOM =
new UnicodeEncoding(false, false);
// Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicode.Equals(unicodeLittleEndianNoBOM));
// Create a UnicodeEncoding to support big-endian byte ordering
// and include the Unicode byte order mark.
UnicodeEncoding unicodeBigEndianBOM =
new UnicodeEncoding(true, true);
// Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicode.Equals(unicodeBigEndianBOM));
// Create a UnicodeEncoding to support big-endian byte ordering
// and not include the Unicode byte order mark.
UnicodeEncoding unicodeBigEndianNoBOM =
new UnicodeEncoding(true, false);
// Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicode.Equals(unicodeBigEndianNoBOM));
}
public static void DescribeEquivalence(Boolean isEquivalent) {
Console.WriteLine(
"{0} equivalent encoding.", (isEquivalent ? "An" : "Not an")
);
}
}
Imports System.Text
Class UnicodeEncodingExample
Public Shared Sub Main()
' Create a UnicodeEncoding without parameters.
Dim unicodeDefault As New UnicodeEncoding()
' Create a UnicodeEncoding to support little-endian byte ordering
' and include the Unicode byte order mark.
Dim unicodeLittleEndianBOM As New UnicodeEncoding(False, True)
' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianBOM))
' Create a UnicodeEncoding to support little-endian byte ordering
' and not include the Unicode byte order mark.
Dim unicodeLittleEndianNoBOM As New UnicodeEncoding(False, False)
' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianNoBOM))
' Create a UnicodeEncoding to support big-endian byte ordering
' and include the Unicode byte order mark.
Dim unicodeBigEndianBOM As New UnicodeEncoding(True, True)
' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianBOM))
' Create a UnicodeEncoding to support big-endian byte ordering
' and not include the Unicode byte order mark.
Dim unicodeBigEndianNoBOM As New UnicodeEncoding(True, False)
' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianNoBOM))
End Sub
Public Shared Sub DescribeEquivalence(isEquivalent As Boolean)
Dim phrase as String
If isEquivalent Then
phrase = "An"
Else
phrase = "Not an"
End If
Console.WriteLine("{0} equivalent encoding.", phrase)
End Sub
End Class
注解
此构造函数创建一个实例,该实例在检测到无效编码时不会引发异常。
Caution
出于安全原因,应通过调用
参数 byteOrderMark 控制方法的操作 GetPreamble 。 如果 true,该方法返回一个字节数组,其中包含 UTF-16 格式的 Unicode 字节顺序标记(BOM)。 如果 false返回零长度字节数组。 但是,设置为byteOrderMarktrue不会导致GetBytes方法在字节数组的开头为 BOM 添加前缀,也不会导致GetByteCount该方法在字节计数中包含 BOM 中的字节数。
另请参阅
适用于
UnicodeEncoding(Boolean, Boolean, Boolean)
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
- Source:
- UnicodeEncoding.cs
初始化 UnicodeEncoding 类的新实例。 参数指定是否使用大字节字节顺序、是否提供 Unicode 字节顺序标记,以及是否在检测到无效编码时引发异常。
public:
UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);
public UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);
new System.Text.UnicodeEncoding : bool * bool * bool -> System.Text.UnicodeEncoding
Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean, throwOnInvalidBytes As Boolean)
参数
- bigEndian
- Boolean
true 使用大字节字节顺序(首先最重要的字节): false 使用小字节字节顺序(第一个最小有效字节)。
- byteOrderMark
- Boolean
- throwOnInvalidBytes
- Boolean
true 指定在检测到无效编码时应引发异常;否则,为 false.
示例
以下示例演示了启用和未启用错误检测的行为 UnicodeEncoding。
using System;
using System.Text;
public class SamplesUnicodeEncoding {
public static void Main() {
// Create an instance of UnicodeEncoding using little-endian byte order.
// This will be used for encoding.
UnicodeEncoding u16LE = new UnicodeEncoding( false, true );
// Create two instances of UnicodeEncoding using big-endian byte order: one with error detection and one without.
// These will be used for decoding.
UnicodeEncoding u16withED = new UnicodeEncoding( true, true, true );
UnicodeEncoding u16noED = new UnicodeEncoding( true, true, false );
// Create byte arrays from the same string containing the following characters:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// Latin Capital Letter U with Diaeresis (U+00DC)
String myStr = "za\u0306\u01FD\u03B2\u00DC";
// Encode the string using little-endian byte order.
byte[] myBytes = new byte[u16LE.GetByteCount( myStr )];
u16LE.GetBytes( myStr, 0, myStr.Length, myBytes, 0 );
// Decode the byte array with error detection.
Console.WriteLine( "Decoding with error detection:" );
PrintDecodedString( myBytes, u16withED );
// Decode the byte array without error detection.
Console.WriteLine( "Decoding without error detection:" );
PrintDecodedString( myBytes, u16noED );
}
// Decode the bytes and display the string.
public static void PrintDecodedString( byte[] bytes, Encoding enc ) {
try {
Console.WriteLine( " Decoded string: {0}", enc.GetString( bytes, 0, bytes.Length ) );
}
catch ( System.ArgumentException e ) {
Console.WriteLine( e.ToString() );
}
Console.WriteLine();
}
}
Imports System.Text
Public Class SamplesUnicodeEncoding
Public Shared Sub Main()
' Create an instance of UnicodeEncoding using little-endian byte order.
' This will be used for encoding.
Dim u16LE As New UnicodeEncoding(False, True)
' Create two instances of UnicodeEncoding using big-endian byte order: one with error detection and one without.
' These will be used for decoding.
Dim u16withED As New UnicodeEncoding(True, True, True)
Dim u16noED As New UnicodeEncoding(True, True, False)
' Create byte arrays from the same string containing the following characters:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' Latin Capital Letter U with Diaeresis (U+00DC)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&H00DC)
' Encode the string using little-endian byte order.
Dim myBytes(u16LE.GetByteCount(myStr)) As Byte
u16LE.GetBytes(myStr, 0, myStr.Length, myBytes, 0)
' Decode the byte array with error detection.
Console.WriteLine("Decoding with error detection:")
PrintDecodedString(myBytes, u16withED)
' Decode the byte array without error detection.
Console.WriteLine("Decoding without error detection:")
PrintDecodedString(myBytes, u16noED)
End Sub
' Decode the bytes and display the string.
Public Shared Sub PrintDecodedString(bytes() As Byte, enc As Encoding)
Try
Console.WriteLine(" Decoded string: {0}", enc.GetString(bytes, 0, bytes.Length))
Catch e As System.ArgumentException
Console.WriteLine(e.ToString())
End Try
Console.WriteLine()
End Sub
End Class
注解
参数 byteOrderMark 控制方法的操作 GetPreamble 。 如果 true,该方法返回一个字节数组,其中包含 UTF-16 格式的 Unicode 字节顺序标记(BOM)。 如果 false返回零长度字节数组。 但是,设置为byteOrderMarktrue不会导致GetBytes方法在字节数组的开头为 BOM 添加前缀,也不会导致GetByteCount该方法在字节计数中包含 BOM 中的字节数。
如果参数 throwOnInvalidBytes 是 true,则检测无效字节序列的方法将 System.ArgumentException引发。 否则,该方法不会引发异常,并且忽略无效序列。
Caution
出于安全原因,应使用此构造函数创建类的