Encoding.GetPreamble 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,返回指定所用编码的字节序列。
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 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 提供前导码,它是一个字节数组,可以作为编码过程中所产生的字节序列的前缀。 如果引言包含字节顺序标记(在 Unicode、码位 U + FEFF 中),则它有助于解码器确定字节顺序和转换格式或 UTF。
Unicode 字节顺序标记(BOM)按以下方式序列化(十六进制):
- UTF-8: EF BB BF 
- UTF-16 大 endian 字节顺序: FE FF 
- UTF-16 little endian 字节顺序: FF FE 
- 32大 endian 字节顺序: 00 00 FE FF 
- 32 little endian 字节顺序: FF FE 00 00 
应使用 BOM,因为它提供了对文件的编码的几乎某些标识,例如,对对象的引用丢失 Encoding 、未标记或标记不正确或标记为不正确的 web 数据或随机文本文件。 如果数据一致且标记正确(最好是 UTF-8 或 UTF-16),通常可以避免用户问题。
对于提供编码类型的标准,BOM 有些多余。 但是,可以使用它来帮助服务器发送正确的编码标头。 或者,它可以用作回退,以防编码在其他情况下丢失。
使用 BOM 存在一些缺点。 例如,了解如何限制使用 BOM 的数据库字段可能很困难。 文件的串联可能也是一个问题,例如,当文件以这样一种方式进行合并时,不需要的字符会在数据中间结束。 但尽管有几个缺点,但强烈建议使用 BOM。
有关字节顺序和字节顺序标记的详细信息,请参阅unicode 主页上的 unicode 标准。
注意
若要确保编码的字节解码正确,应使用前导码作为编码字节的前缀。 但是,大多数编码不提供前导码。 若要确保已编码的字节解码正确,应使用 Unicode 编码,即、、 UTF8Encoding UnicodeEncoding 或 UTF32Encoding ,使用前导码。