C# Encoding GetPreamble

Description

Encoding GetPreamble When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.

Syntax

Encoding.GetPreamble has the following syntax.


public virtual byte[] GetPreamble()

Returns

Encoding.GetPreamble method returns

Example

The following example determines the byte order of the encoding based on the preamble.


/*  w  w  w  .  ja  v  a2 s. com*/
using System;
using System.Text;

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

      byte[] preamble = unicode.GetPreamble();
      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.");
         }
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding