Encoding.GetPreamble 方法

定義

在衍生類別中覆寫時,傳回可指定所用編碼方式的位元組序列。When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.

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

位元組陣列,包含可指定所用編碼方式的位元組序列。A byte array containing a sequence of bytes that specifies the encoding used.

-或--or-

如果不需要前序編碼,則位元組陣列的長度為零。A byte array of length zero, if a preamble is not required.

範例

下列範例會根據前序判斷編碼的位元組順序。The following example determines the byte order of the encoding based on the preamble.

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 containa 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 containa 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物件會提供前置字元,其為位元組陣列,可在編碼程式所產生的位元組序列前面加上。Optionally, the Encoding object provides a preamble that is an array of bytes that can be prefixed to the sequence of bytes resulting from the encoding process. 如果前序包含位元組順序標記(Unicode、程式碼點 U + FEFF),它會協助解碼器判斷位元組順序和轉換格式或 UTF。If the preamble contains a byte order mark (in Unicode, code point U+FEFF), it helps the decoder determine the byte order and the transformation format or UTF.

Unicode 位元組順序標記(BOM)會序列化如下(十六進位):The Unicode byte order mark (BOM) is serialized as follows (in hexadecimal):

  • UTF-8:EF BB BFUTF-8: EF BB BF

  • UTF-16 位元組由大到小的順序:FE FFUTF-16 big endian byte order: FE FF

  • UTF-16 位元組由大到小的順序:FF FEUTF-16 little endian byte order: FF FE

  • UTF-32 big endian 位元組順序:00 00 FE FFUTF-32 big endian byte order: 00 00 FE FF

  • UTF-32 位元組由小到大的位元組順序:FF FE 00 00UTF-32 little endian byte order: FF FE 00 00

您應該使用 BOM,因為它幾乎會針對可能遺失Encoding物件參考的檔案提供編碼的特定識別,例如,未標記或標記不正確的 web 資料,或在企業未儲存的隨機文字檔有國際疑慮或其他資料。You should use the BOM, because it provides nearly certain identification of an encoding for files that otherwise have lost reference to the Encoding object, for example, untagged or improperly tagged web data or random text files stored when a business did not have international concerns or other data. 如果資料一致且正確地加上標籤,通常是採用 UTF-8 或 UTF-16,可能就會避免使用者的問題。Often user problems might be avoided if data is consistently and properly tagged, preferably in UTF-8 or UTF-16.

針對提供編碼類型的標準,BOM 有點重複。For standards that provide an encoding type, a BOM is somewhat redundant. 不過,它可以用來協助伺服器傳送正確的編碼標頭。However, it can be used to help a server send the correct encoding header. 或者,也可以在編碼方式遺失時,做為回退使用。Alternatively, it can be used as a fallback in case the encoding is otherwise lost.

使用 BOM 有一些缺點。There are some disadvantages to using a BOM. 例如,知道如何限制使用 BOM 的資料庫欄位可能會很棘手。For example, knowing how to limit the database fields that use a BOM can be difficult. 檔案的串連可能也是問題,例如,合併檔案時,不必要的字元可能會在資料中間結束。Concatenation of files can be a problem also, for example, when files are merged in such a way that an unnecessary character can end up in the middle of data. 不過,儘管有幾個缺點,但強烈建議使用 BOM。In spite of the few disadvantages, however, the use of a BOM is highly recommended.

如需位元組順序和位元組順序標記的詳細資訊,請參閱unicode 首頁的 unicode 標準。For more information on byte order and the byte order mark, see The Unicode Standard at the Unicode home page.

警告

為確保已正確解碼編碼的位元組,您應該在編碼的位元組前面加上前置字元。To ensure that the encoded bytes are decoded properly, you should prefix encoded bytes with a preamble. 不過,大部分的編碼並不會提供前序。However, most encodings do not provide a preamble. 為確保已正確解碼編碼的位元組,您應該使用 Unicode 編碼,也就是UTF8Encoding UnicodeEncoding、或UTF32Encoding,並搭配前序。To ensure that the encoded bytes are decoded properly, you should use a Unicode encoding, that is, UTF8Encoding, UnicodeEncoding, or UTF32Encoding, with a preamble.

適用於