UnicodeEncoding 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 UnicodeEncoding 類別的新執行個體。
多載
UnicodeEncoding() |
初始化 UnicodeEncoding 類別的新執行個體。 |
UnicodeEncoding(Boolean, Boolean) |
初始化 UnicodeEncoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序,以及 GetPreamble() 方法是否傳回 Unicode 位元組順序標記。 |
UnicodeEncoding(Boolean, Boolean, Boolean) |
初始化 UnicodeEncoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序、是否提供 Unicode 位元組順序標記,以及是否在偵測到無效的編碼方式時擲回例外狀況。 |
UnicodeEncoding()
初始化 UnicodeEncoding 類別的新執行個體。
public:
UnicodeEncoding();
public UnicodeEncoding ();
Public Sub New ()
範例
下列範例示範如何建立新的 UnicodeEncoding 實例,並顯示編碼的名稱。
using namespace System;
using namespace System::Text;
int main()
{
UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
String^ encodingName = unicode->EncodingName;
Console::WriteLine( "Encoding name: {0}", encodingName );
}
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 位元組順序標記,而且在偵測到無效編碼時不會擲回例外狀況。
警告
基於安全性考慮,您應該呼叫 建構函式並將其 UnicodeEncoding(Boolean, Boolean, Boolean) 引數設定 throwOnInvalidBytes
為 true
來啟用錯誤偵測。
適用於
UnicodeEncoding(Boolean, Boolean)
初始化 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
true
指定 GetPreamble() 方法會傳回 Unicode 位元組順序標記;否則為 false
。
範例
下列範例示範如何建立新的 UnicodeEncoding 實例,指定是否支援小端或大位元組位元組順序和 Unicode 位元組順序標記。
using namespace System;
using namespace System::Text;
void DescribeEquivalence( Boolean isEquivalent )
{
Console::WriteLine( " {0} equivalent encoding.", (isEquivalent ? (String^)"An" : "Not an") );
}
int main()
{
// Create a UnicodeEncoding without parameters.
UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
// Create a UnicodeEncoding to support little-endian Byte ordering
// and include the Unicode Byte order mark.
UnicodeEncoding^ unicodeLittleEndianBOM = gcnew 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 = gcnew 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 = gcnew 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 = gcnew UnicodeEncoding( true,false );
// Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
DescribeEquivalence( unicode->Equals( unicodeBigEndianNoBOM ) );
}
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
備註
這個建構函式會建立一個實例,在偵測到不正確編碼時不會擲回例外狀況。
警告
基於安全性考慮,您應該呼叫 建構函式並將其 UnicodeEncoding(Boolean, Boolean, Boolean) 引數設定 throwOnInvalidBytes
為 true
來啟用錯誤偵測。
參數 byteOrderMark
會控制 方法的 GetPreamble 作業。 如果 true
為 ,則方法會傳回位元組陣列,其中包含 UTF-16 格式的 Unicode 位元組順序標記 (BOM) 。 如果 false
為 ,則會傳回長度為零的位元組陣列。 不過,設定 byteOrderMark
為 不會讓 GetBytes 方法在位元組陣列開頭加上 BOM,也不會使 GetByteCount 方法在位元組計數中包含 BOM true
中的位元組數目。
另請參閱
適用於
UnicodeEncoding(Boolean, Boolean, Boolean)
初始化 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
true
指定 GetPreamble() 方法會傳回 Unicode 位元組順序標記;否則為 false
。
- throwOnInvalidBytes
- Boolean
true
,可指定在偵測到無效的編碼方式時應擲回一個例外狀況,否則為 false
。
範例
下列範例示範 UnicodeEncoding 的行為,兩者皆已啟用錯誤偵測,且不含 。
using namespace System;
using namespace System::Text;
void PrintDecodedString( array<Byte>^bytes, Encoding^ enc );
int main()
{
// Create an instance of UnicodeEncoding using little-endian byte order.
// This will be used for encoding.
UnicodeEncoding^ u16LE = gcnew 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 = gcnew UnicodeEncoding( true,true,true );
UnicodeEncoding^ u16noED = gcnew 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.
array<Byte>^myBytes = gcnew array<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.
void PrintDecodedString( array<Byte>^bytes, Encoding^ enc )
{
try
{
Console::WriteLine( " Decoded string: {0}", enc->GetString( bytes, 0, bytes->Length ) );
}
catch ( System::ArgumentException^ e )
{
Console::WriteLine( e );
}
Console::WriteLine();
}
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
為 ,則會傳回長度為零的位元組陣列。 不過,設定 byteOrderMark
為 不會讓 GetBytes 方法在位元組陣列開頭加上 BOM,也不會使 GetByteCount 方法在位元組計數中包含 BOM true
中的位元組數目。
throwOnInvalidBytes
如果 參數為 true
,則偵測無效位元組序列的方法會 System.ArgumentException 擲回 。 否則,方法不會擲回例外狀況,而且會忽略不正確序列。
警告
基於安全性考慮,您應該使用此建構函式來建立 類別的 UnicodeEncoding 實例,並將 設定 throwOnInvalidBytes
為 true
來開啟錯誤偵測。