Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

UnicodeEncoding Constructor (Boolean, Boolean)

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big-endian byte order and whether to provide a Unicode byte order mark.

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

public UnicodeEncoding (
	bool bigEndian,
	bool byteOrderMark
)
public UnicodeEncoding (
	boolean bigEndian, 
	boolean byteOrderMark
)
public function UnicodeEncoding (
	bigEndian : boolean, 
	byteOrderMark : boolean
)

Parameters

bigEndian

true to use the big-endian byte order (most significant byte first), or false to use the little-endian byte order (least significant byte first).

byteOrderMark

true to specify that a Unicode byte order mark is provided; otherwise, false.

This constructor creates an instance that does not throw an exception when an invalid encoding is detected.

Caution noteCaution

For security reasons, it is recommended that you enable error detection by using the constructor that accepts a throwOnInvalidBytes parameter and set that parameter to true.

The encoder can use the big-endian byte order (most significant byte first) or the little-endian byte order (least significant byte first). For example, the Latin Capital Letter A (code point U+0041) is serialized as follows (in hexadecimal):

  • Big-endian byte order: 00 41

  • Little-endian byte order: 41 00

Optionally, the UnicodeEncoding provides a preamble, which is an array of bytes that you can prefix to the sequence of bytes resulting from the encoding process. If the preamble contains a byte order mark (code point U+FEFF), it helps the decoder determine the byte order and the transformation format or UTF. The Unicode byte order mark is serialized as follows (in hexadecimal):

  • Big-endian byte order: FE FF

  • Little-endian byte order: FF FE

It is generally more efficient to store Unicode characters using the native byte order. For example, it is better to use the little-endian byte order on little-endian platforms, such as Intel machines.

For more information about byte order and the byte order mark, see The Unicode Standard at www.unicode.org.

The following code example demonstrates how to create a new UnicodeEncoding instance specifying whether to support little-endian or big-endian byte ordering and the Unicode byte order mark. Depending on the values of the bigEndian and byteOrderMark parameters, the UnicodeEncoding instance behaves differently.

using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {

        // Create a UnicodeEncoding without parameters.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianBOM = 
            new UnicodeEncoding(false, true);
        // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianBOM));

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianNoBOM =
            new UnicodeEncoding(false, false);
        // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianNoBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianBOM =
            new UnicodeEncoding(true, true);
        // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianNoBOM =
            new UnicodeEncoding(true, false);
        // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianNoBOM));
    }

    public static void DescribeEquivalence(Boolean isEquivalent) {
        Console.WriteLine(
            "{0} equivalent encoding.", (isEquivalent ? "An" : "Not an")
        );
    }
}

import System.*;
import System.Text.*;
import System.Boolean;

class UnicodeEncodingExample
{
    public static void main(String[] args)
    {
        // Create a UnicodeEncoding without parameters.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianBOM = new 
            UnicodeEncoding(false, true);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianBOM));

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianNoBOM = new 
            UnicodeEncoding(false, false);

        // Compare this UnicodeEncoding to the UnicodeEncoding 
        // without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianNoBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianBOM = new UnicodeEncoding(true, true);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianNoBOM = new 
            UnicodeEncoding(true, false);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianNoBOM));
    } //main

    public static void DescribeEquivalence(boolean isEquivalent)
    {
        Console.WriteLine("{0} equivalent encoding.",
            (isEquivalent) ? "An" : "Not an");
    } //DescribeEquivalence
} //UnicodeEncodingExample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.