UnicodeEncoding 建構函式

定義

初始化 UnicodeEncoding 類別的新執行個體。

多載

名稱 Description
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 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, Boolean) 建構子並將其參數設 throwOnInvalidBytestrue來啟用錯誤偵測。

適用於

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 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

出於安全考量,你應該透過呼叫 UnicodeEncoding(Boolean, Boolean, Boolean) 建構子並將其參數設 throwOnInvalidBytestrue來啟用錯誤偵測。

參數 byteOrderMark 控制方法的 GetPreamble 運作。 若 true,該方法會回傳包含 Unicode 位元組順序標記(BOM)的位元組陣列,格式為 UTF-16。 若 false,則回傳一個零長度的位元組陣列。 然而,設定 byteOrderMarktrue 並不會使 GetBytes 方法在位元組陣列開頭加上 BOM,也不會將 GetByteCount BOM 中的位元組數納入位元組計數。

另請參閱

適用於

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 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,該方法會回傳包含 Unicode 位元組順序標記(BOM)的位元組陣列,格式為 UTF-16。 若 false,則回傳一個零長度的位元組陣列。 然而,設定 byteOrderMarktrue 並不會使 GetBytes 方法在位元組陣列開頭加上 BOM,也不會將 GetByteCount BOM 中的位元組數納入位元組計數。

throwOnInvalidBytes 參數為 true,偵測無效位元組序列的方法會拋 System.ArgumentException出 。 否則,方法不會拋出例外,且無效序列會被忽略。

Caution

出於安全考量,你應該使用這個建構子來建立該 UnicodeEncoding 類別的實例,並透過設定 throwOnInvalidBytestrue來開啟錯誤偵測。

另請參閱

適用於