Encoding: GetPreamble

GetPreamble

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



 Public Function GetPreamble ( ) As Byte ( ) :

Return Values

Byte() -  A byte array containg the bytes to be used to identify this encoding type.
-or-
A byte array of length zero, if a preamble is not required.

Remarks

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. 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):

Your applications are recommended to 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. Often user problems might be avoided if data is consistently and properly tagged, preferably in UTF-8 or UTF-16.

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
To ensure that the encoded bytes are decoded properly, your application should prefix encoded bytes with a preamble. However, most encodings do not provide a preamble. To ensure that the encoded bytes are decoded properly, the application should use a Unicode encoding, that is, UTF8Encoding, UnicodeEncoding, or UTF32Encoding, with a preamble.

Examples

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

Public Sub Main()
    Dim Unicode As Encoding
    Dim Preamble() As Byte
    
    Set Unicode = Encoding.Unicode
    
    ' Get the preamble for the Unicode encoder.
    ' In this case the preamble contains the byte order mark (BOM).
    Preamble = Unicode.GetPreamble
    
    ' Make sure a preamble was returned
    ' and is large enough to contain a BOM.
    If CorArray.Length(Preamble) >= 2 Then
        If Preamble(0) = &HFE And Preamble(1) = &HFF Then
            Debug.Print "The Unicode encoder is encoding in big-endian order."
        ElseIf Preamble(0) = &HFF And Preamble(1) = &HFE Then
            Debug.Print "The Unicode encoder is encoding in little-endian order."
        End If
    End If
End Sub

' This code produces the following output.
'
'   The Unicode encoder is encoding in little-endian order.

See Also

Project CorLib Overview

Class Encoding Overview