UTF32Encoding 建構函式

定義

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

多載

UTF32Encoding()

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

UTF32Encoding(Boolean, Boolean)

初始化 UTF32Encoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序,以及 GetPreamble() 方法是否傳回 Unicode 位元組順序標記。

UTF32Encoding(Boolean, Boolean, Boolean)

初始化 UTF32Encoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序、是否提供 Unicode 位元組順序標記,以及是否在偵測到無效的編碼方式時擲回例外狀況。

UTF32Encoding()

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

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

public UTF32Encoding ();

範例

下列範例會擷取並顯示不同 UTF32Encoding 實例的位元組順序標記。

using System;
using System.Text;

public class SamplesUTF32Encoding
{
   public static void Main()
   {
      // Create instances of UTF32Encoding, with the byte order mark and without.
      UTF32Encoding u32LeNone = new UTF32Encoding();
      UTF32Encoding u32BeNone = new UTF32Encoding( true, false );
      UTF32Encoding u32LeBom  = new UTF32Encoding( false, true );
      UTF32Encoding u32BeBom  = new UTF32Encoding( true, true );

      // Display the preamble for each instance.
      PrintHexBytes( u32LeNone.GetPreamble() );
      PrintHexBytes( u32BeNone.GetPreamble() );
      PrintHexBytes( u32LeBom.GetPreamble() );
      PrintHexBytes( u32BeBom.GetPreamble() );
   }

   public static void PrintHexBytes( byte[] bytes )
   {

      if (( bytes == null ) || ( bytes.Length == 0 ))
        {
            Console.WriteLine( "<none>" );
        }
        else  {
         for ( int i = 0; i < bytes.Length; i++ )
            Console.Write( "{0:X2} ", bytes[i] );
         Console.WriteLine();
      }
   }
}
/*
This example displays the following output.
      FF FE 00 00
      <none>
      FF FE 00 00
      00 00 FE FF
*/

備註

這個建構函式會建立一個實例,該實例使用位元組的位元組順序、提供 Unicode 位元組順序標記,而且在偵測到不正確編碼時不會擲回例外狀況。

警告

基於安全性考慮,您應該呼叫 建構函式並將其 UTF32Encoding(Boolean, Boolean, Boolean) 引數設定 throwOnInvalidCharacterstrue 來啟用錯誤偵測。

適用於

UTF32Encoding(Boolean, Boolean)

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

初始化 UTF32Encoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序,以及 GetPreamble() 方法是否傳回 Unicode 位元組順序標記。

public UTF32Encoding (bool bigEndian, bool byteOrderMark);

參數

bigEndian
Boolean

true 表示要使用位元組由大到小的位元組順序 (最大顯著性位元組在前),false 表示要使用位元組由小到大的位元組順序 (最小顯著性位元組在前)。

byteOrderMark
Boolean

true 指定提供 Unicode 位元組順序標記;否則為 false

範例

下列範例會擷取並顯示不同 UTF32Encoding 實例的位元組順序標記。

using System;
using System.Text;

public class SamplesUTF32Encoding
{
   public static void Main()
   {
      // Create instances of UTF32Encoding, with the byte order mark and without.
      UTF32Encoding u32LeNone = new UTF32Encoding();
      UTF32Encoding u32BeNone = new UTF32Encoding( true, false );
      UTF32Encoding u32LeBom  = new UTF32Encoding( false, true );
      UTF32Encoding u32BeBom  = new UTF32Encoding( true, true );

      // Display the preamble for each instance.
      PrintHexBytes( u32LeNone.GetPreamble() );
      PrintHexBytes( u32BeNone.GetPreamble() );
      PrintHexBytes( u32LeBom.GetPreamble() );
      PrintHexBytes( u32BeBom.GetPreamble() );
   }

   public static void PrintHexBytes( byte[] bytes )
   {

      if (( bytes == null ) || ( bytes.Length == 0 ))
        {
            Console.WriteLine( "<none>" );
        }
        else  {
         for ( int i = 0; i < bytes.Length; i++ )
            Console.Write( "{0:X2} ", bytes[i] );
         Console.WriteLine();
      }
   }
}
/*
This example displays the following output.
      FF FE 00 00
      <none>
      FF FE 00 00
      00 00 FE FF
*/

備註

此建構函式會建立實例,在偵測到不正確編碼時不會擲回例外狀況。

警告

基於安全性考慮,您應該呼叫 建構函式並將其 UTF32Encoding(Boolean, Boolean, Boolean) 引數設定 throwOnInvalidCharacterstrue 來啟用錯誤偵測。

另請參閱

適用於

UTF32Encoding(Boolean, Boolean, Boolean)

來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs
來源:
UTF32Encoding.cs

初始化 UTF32Encoding 類別的新執行個體。 參數會指定是否使用位元組由大到小的位元組順序、是否提供 Unicode 位元組順序標記,以及是否在偵測到無效的編碼方式時擲回例外狀況。

public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters);

參數

bigEndian
Boolean

true 表示要使用位元組由大到小的位元組順序 (最大顯著性位元組在前),false 表示要使用位元組由小到大的位元組順序 (最小顯著性位元組在前)。

byteOrderMark
Boolean

true 指定提供 Unicode 位元組順序標記;否則為 false

throwOnInvalidCharacters
Boolean

true,可指定在偵測到無效的編碼方式時應擲回一個例外狀況,否則為 false

範例

下列範例示範 UTF32Encoding 的行為,同時啟用和不使用錯誤偵測。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
     // Create a UTF32Encoding object with error detection enabled.
      var encExc = new UTF32Encoding(! BitConverter.IsLittleEndian, true, true);
      // Create a UTF32Encoding object with error detection disabled.
      var encRepl = new UTF32Encoding(! BitConverter.IsLittleEndian, true, false);

      // Create a byte arrays from a string, and add an invalid surrogate pair, as follows.
      //    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)
      //    a high-surrogate value (U+D8FF)
      //    an invalid low surrogate (U+01FF)
      String s = "za\u0306\u01FD\u03B2";

      // Encode the string using little-endian byte order.
      int index = encExc.GetByteCount(s);
      Byte[] bytes = new Byte[index + 4];
      encExc.GetBytes(s, 0, s.Length, bytes, 0);
      bytes[index] = 0xFF;
      bytes[index + 1] = 0xD8;
      bytes[index + 2] = 0xFF;
      bytes[index + 3] = 0x01;

      // Decode the byte array with error detection.
      Console.WriteLine("Decoding with error detection:");
      PrintDecodedString(bytes, encExc);

      // Decode the byte array without error detection.
      Console.WriteLine("Decoding without error detection:");
      PrintDecodedString(bytes, encRepl);
   }

   // 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 (DecoderFallbackException e) {
         Console.WriteLine(e.ToString());
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//    Decoding with error detection:
//    System.Text.DecoderFallbackException: Unable to translate bytes [FF][D8][FF][01] at index
//    20 from specified code page to Unicode.
//       at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
//       at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index
//    )
//       at System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes)
//       at System.Text.UTF32Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDeco
//    der)
//       at System.Text.UTF32Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
//       at Example.PrintDecodedString(Byte[] bytes, Encoding enc)
//
//    Decoding without error detection:
//       Decoded string: zăǽβ�

備註

如果 throwOnInvalidCharacterstrue ,則偵測無效位元組序列的方法會 System.ArgumentException 擲回 。 否則,方法不會擲回例外狀況,而且會忽略不正確序列。

警告

基於安全性考慮,您應該呼叫 建構函式並將其 UTF32Encoding(Boolean, Boolean, Boolean) 引數設定 throwOnInvalidCharacterstrue 來啟用錯誤偵測。

另請參閱

適用於