Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

UnicodeEncoding.GetPreamble Method

Returns a Unicode byte order mark encoded in big-endian or little-endian format, if the constructor for this instance requested byte order mark support.

[Visual Basic]
Overrides Public Function GetPreamble() As Byte()
[C#]
public override byte[] GetPreamble();
[C++]
public: unsigned char GetPreamble()  __gc[];
[JScript]
public override function GetPreamble() : Byte[];

Return Value

If the constructor for this instance did not request byte order mark support, that is the byteOrderMark parameter of the UnicodeEncoding constructor is false, then a byte array of length zero.

Otherwise, if the constructor requested big-endian byte order mark support then a byte array whose elements contain hexadecimal 0xFE, 0xFF; or if the constructor requested little-endian byte order mark support then a byte array whose elements contain hexadecimal 0xFF, 0xFE.

Remarks

The byte order mark (U+FEFF), when concatenated to the beginning of a UTF-16 encoding, enables a class like System.IO.StreamReader to deduce whether the remainder of the encoding is in big-endian or little-endian format.

Concatenate this return value to the beginning of a UTF-16 encoding if the constructor for this instance requested support for a byte order mark.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetPreamble method to return the Unicode byte order mark in big-endian or little-endian format for an instance of a UnicodeEncoding.

[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim byteOrderMark() As Byte
        Dim b As Byte
        
        byteOrderMark = Encoding.Unicode.GetPreamble()
        Console.WriteLine("Default (little-endian) Unicode Preamble:")
        For Each b In  byteOrderMark
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine(ControlChars.NewLine)
        
        Dim bigEndianUnicode As New UnicodeEncoding(True, True)
        byteOrderMark = bigEndianUnicode.GetPreamble()
        Console.WriteLine("Big-endian Unicode Preamble:")
        For Each b In  byteOrderMark
            Console.Write("[{0}]", b)
        Next b
    End Sub 'Main
End Class 'UnicodeEncodingExample

[C#] 
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] byteOrderMark;
        
        byteOrderMark = Encoding.Unicode.GetPreamble();
        Console.WriteLine("Default (little-endian) Unicode Preamble:");
        foreach (Byte b in byteOrderMark) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine("\n");

        UnicodeEncoding bigEndianUnicode = new UnicodeEncoding(true, true);
        byteOrderMark = bigEndianUnicode.GetPreamble();
        Console.WriteLine("Big-endian Unicode Preamble:");
        foreach (Byte b in byteOrderMark) {
            Console.Write("[{0}]", b);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main()
{
   Byte byteOrderMark[];

   byteOrderMark = Encoding::Unicode -> GetPreamble();
   Console::WriteLine(S"Default (little-endian) Unicode Preamble:");
   IEnumerator* myEnum = byteOrderMark->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
      Console::Write(S"[{0}]", __box(b));
   }
   Console::WriteLine(S"\n");

   UnicodeEncoding* bigEndianUnicode = new UnicodeEncoding(true, true);
   byteOrderMark = bigEndianUnicode -> GetPreamble();
   Console::WriteLine(S"Big-endian Unicode Preamble:");
   myEnum = byteOrderMark->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
      Console::Write(S"[{0}]", __box(b));
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace

Show:
© 2015 Microsoft