Encoding.GetPreamble 方法

定義

在衍生類別中覆寫時,傳回可指定所用編碼方式的位元組序列。

public:
 virtual cli::array <System::Byte> ^ GetPreamble();
public virtual byte[] GetPreamble ();
abstract member GetPreamble : unit -> byte[]
override this.GetPreamble : unit -> byte[]
Public Overridable Function GetPreamble () As Byte()

傳回

Byte[]

位元組陣列,包含可指定所用編碼方式的位元組序列。

-或-

如果不需要前序編碼,則位元組陣列的長度為零。

範例

下列範例會根據前置詞來決定編碼的位元組順序。

using namespace System;
using namespace System::Text;
int main()
{
   Encoding^ unicode = Encoding::Unicode;
   
   // Get the preamble for the Unicode encoder. 
   // In this case the preamblecontains the Byte order mark (BOM).
   array<Byte>^preamble = unicode->GetPreamble();
   
   // Make sure a preamble was returned 
   // and is large enough to contain a BOM.
   if ( preamble->Length >= 2 )
   {
      
      // if (preamble->Item[0] == 0xFE && preamble->Item[1] == 0xFF) 
      if ( preamble[ 0 ] == 0xFE && preamble[ 1 ] == 0xFF )
      {
         Console::WriteLine( "The Unicode encoder is encoding in big-endian order." );
      }
      // else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE) 
      else
      
      // else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE) 
      if ( preamble[ 0 ] == 0xFF && preamble[ 1 ] == 0xFE )
      {
         Console::WriteLine( "The Unicode encoder is encoding in little-endian order." );
      }
   }
}

/*
This code produces the following output.

The Unicode encoder is encoding in little-endian order.

*/
using System;
using System.Text;

namespace GetPreambleExample
{
   class GetPreambleExampleClass
   {
      static void Main()
      {
         Encoding unicode = Encoding.Unicode;

         // Get the preamble for the Unicode encoder. 
         // In this case the preamble contains the byte order mark (BOM).
         byte[] preamble = unicode.GetPreamble();

         // Make sure a preamble was returned 
         // and is large enough to contain a BOM.
         if(preamble.Length >= 2)
         {
            if(preamble[0] == 0xFE && preamble[1] == 0xFF)
            {
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.");
            }
            else if(preamble[0] == 0xFF && preamble[1] == 0xFE)
            {
               Console.WriteLine("The Unicode encoder is encoding in little-endian order.");
            }
         }
      }
   }
}

/*
This code produces the following output.

The Unicode encoder is encoding in little-endian order.

*/
Imports System.Text

Namespace GetPreambleExample
   Class GetPreambleExampleClass
      Shared Sub Main()
         Dim [unicode] As Encoding = Encoding.Unicode

         ' Get the preamble for the Unicode encoder. 
         ' In this case the preamble contains the byte order mark (BOM).
         Dim preamble As Byte() = [unicode].GetPreamble()

         ' Make sure a preamble was returned 
         ' and is large enough to contain a BOM.
         If preamble.Length >= 2 Then
            If preamble(0) = &HFE And preamble(1) = &HFF Then
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.")
            Else
               If preamble(0) = &HFF And preamble(1) = &HFE Then
                  Console.WriteLine("The Unicode encoder is encoding in little-endian order.")
               End If
            End If
         End If
      End Sub
   End Class
End Namespace

'This code produces the following output.
'
'The Unicode encoder is encoding in little-endian order.
'

備註

或者, Encoding 物件會提供前置詞,這個前置詞是位元組陣列,可以前置詞到編碼程式所產生的位元組序列。 如果前置詞包含 Unicode 中的位元組順序標記 (,則代碼點 U+FEFF) ,可協助解碼器判斷位元組順序和轉換格式或 UTF。

Unicode 位元組順序標記 (BOM) 序列化,如下所示 (十六進位) :

  • UTF-8:EF BB BF

  • UTF-16 大位元組順序:FE FF

  • UTF-16 位元組順序:FF FE

  • UTF-32 大位元組位元組順序:00 00 FE FF

  • UTF-32 位元組位元組順序:FF FE 00 00

您應該使用 BOM,因為它為檔案提供幾乎確定的編碼 Encoding 方式識別,例如,當企業沒有國際疑慮或其他資料時,未標記或未正確標記的 Web 資料或隨機文字檔。 如果資料一致且正確標記,最好在 UTF-8 或 UTF-16 中避免使用者問題。

對於提供編碼類型的標準,BOM 有點重複。 不過,它可用來協助伺服器傳送正確的編碼標頭。 或者,當編碼遺失時,可以使用它做為後援。

使用 BOM 有一些缺點。 例如,瞭解如何限制使用 BOM 的資料庫欄位可能很困難。 檔案串連也可能是問題,例如,當檔案合併時,可能會讓不必要的字元最終出現在資料中間。 不過,雖然有幾個缺點,但強烈建議使用 BOM。

如需位元組順序和位元組順序標記的詳細資訊,請參閱 Unicode 首頁上的 Unicode標準。

警告

若要確保編碼的位元組已正確解碼,您應該使用前置詞為編碼的位元組加上前置詞。 不過,大部分的編碼不會提供前置詞。 若要確保編碼的位元組已正確解碼,您應該使用 Unicode 編碼,也就是 、 UTF8EncodingUnicodeEncoding 、 或 UTF32Encoding ,且具有前置詞。

適用於