UnicodeEncoding Constructor (Boolean, Boolean, Boolean)

July 28, 2014

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big-endian byte order, whether to provide a Unicode byte order mark, and whether to throw an exception when an invalid encoding is detected.

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

public UnicodeEncoding(
	bool bigEndian,
	bool byteOrderMark,
	bool throwOnInvalidBytes
)

Parameters

bigEndian
Type: System.Boolean
true to use the big-endian byte order (most significant byte first); false to use the little-endian byte order (least significant byte first).
byteOrderMark
Type: System.Boolean
true to specify that a Unicode byte order mark is provided; otherwise, false.
throwOnInvalidBytes
Type: System.Boolean
true to specify that an exception should be thrown when an invalid encoding is detected; otherwise, false.

It is generally more efficient to encode 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 computers, and the big-endian byte order on big-endian platforms.

If the throwOnInvalidBytes parameter is true, a method that detects an invalid byte sequence throws System.ArgumentException. Otherwise, the method does not throw an exception, and the invalid sequence is ignored.

NoteNote:

For security reasons, your applications are recommended to use this constructor to create an instance of the UnicodeEncoding class and turn on error detection by setting throwOnInvalidBytes to true.

The following code example demonstrates the behavior of UnicodeEncoding, both with error detection enabled and without.


using System;
using System.Text;

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Create an instance of UnicodeEncoding using little-endian byte order.
      // This will be used for encoding.
      UnicodeEncoding u16LE = new UnicodeEncoding(false, true);

      // Create two instances of UnicodeEncoding using big-endian byte order: one with error detection and one without.
      // These will be used for decoding.
      UnicodeEncoding u16withED = new UnicodeEncoding(true, true, true);
      UnicodeEncoding u16noED = new UnicodeEncoding(true, true, false);

      // Create byte arrays from the same string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      //    Latin Capital Letter U with  Diaeresis (U+00DC)
      String myStr = "za\u0306\u01FD\u03B2\u00DC";

      // Encode the string using little-endian byte order.
      byte[] myBytes = new byte[u16LE.GetByteCount(myStr)];
      u16LE.GetBytes(myStr, 0, myStr.Length, myBytes, 0);

      // Decode the byte array with error detection.
      outputBlock.Text += "Decoding with error detection:" + "\n";
      PrintDecodedString(outputBlock, myBytes, u16withED);

      // Decode the byte array without error detection.
      outputBlock.Text += "Decoding without error detection:" + "\n";
      PrintDecodedString(outputBlock, myBytes, u16noED);

   }


   // Decode the bytes and display the string.
   public static void PrintDecodedString(System.Windows.Controls.TextBlock outputBlock, byte[] bytes, Encoding enc)
   {

      try
      {
         outputBlock.Text += String.Format("   Decoded string: {0}", enc.GetString(bytes, 0, bytes.Length)) + "\n";
      }
      catch (System.ArgumentException e)
      {
         outputBlock.Text += e.ToString() + "\n";
      }

      outputBlock.Text += "\n";

   }

}



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft