GetPreamble Method

UnicodeEncoding.GetPreamble Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns a Unicode byte order mark encoded in UTF-16 format.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

public override byte[] GetPreamble()

Return Value

Type: System.Byte []
A byte array containing the Unicode byte order mark, if the constructor for this instance requests a byte order mark. Otherwise, this method returns a zero-length byte array.

Optionally, the UnicodeEncoding object provides a preamble that is an array of bytes that can be prefixed to the sequence of bytes resulting from the encoding process. 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.

The Unicode byte order mark (BOM) is serialized as follows (in hexadecimal):

  • Big-endian byte order: 00 00 FE FF

  • Little-endian byte order: FF FE 00 00

Your applications are recommended to use the BOM, as it provides nearly certain identification of an encoding for files that otherwise have lost reference to the UnicodeEncoding 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. Often user problems might be avoided if data is consistently and properly tagged.

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.

There are some disadvantages to using a 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. In spite of the few disadvantages, however, the use of a BOM is highly recommended.

For more information on byte order and the byte order mark, see The Unicode Standard at the Unicode home page.

Caution noteCaution:

To ensure that the encoded bytes are decoded properly, your application should prefix encoded bytes with a preamble.

The following code example demonstrates how to use the GetPreamble method to retrieve the Unicode byte order mark in big-endian or little-endian byte order for an instance of a UnicodeEncoding.


using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Byte[] byteOrderMark;

      byteOrderMark = Encoding.Unicode.GetPreamble();
      outputBlock.Text += "Default (little-endian) Unicode Preamble:" + "\n";
      foreach (Byte b in byteOrderMark)
      {
         outputBlock.Text += String.Format("[{0}]", b);
      }
      outputBlock.Text += "\n" + "\n";

      UnicodeEncoding bigEndianUnicode = new UnicodeEncoding(true, true);
      byteOrderMark = bigEndianUnicode.GetPreamble();
      outputBlock.Text += "Big-endian Unicode Preamble:" + "\n";
      foreach (Byte b in byteOrderMark)
      {
         outputBlock.Text += String.Format("[{0}]", b);
      }
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft