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 Class

Represents a UTF-16 encoding of Unicode characters.

System.Object
  System.Text.Encoding
    System.Text.UnicodeEncoding

Namespace:  System.Text
Assemblies:   System.Text.Encoding.Extensions (in System.Text.Encoding.Extensions.dll)
  mscorlib (in mscorlib.dll)

public class UnicodeEncoding : Encoding

The UnicodeEncoding type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryUnicodeEncoding()Initializes a new instance of the UnicodeEncoding class.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryUnicodeEncoding(Boolean, Boolean)Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order and whether the GetPreamble method returns a Unicode byte order mark.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryUnicodeEncoding(Boolean, Boolean, Boolean)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.
Top

  NameDescription
Public propertyBodyNameWhen overridden in a derived class, gets a name for the current encoding that can be used with mail agent body tags. (Inherited from Encoding.)
Public propertySupported by the XNA FrameworkCodePageWhen overridden in a derived class, gets the code page identifier of the current Encoding. (Inherited from Encoding.)
Public propertyDecoderFallbackGets or sets the DecoderFallback object for the current Encoding object. (Inherited from Encoding.)
Public propertyEncoderFallbackGets or sets the EncoderFallback object for the current Encoding object. (Inherited from Encoding.)
Public propertyEncodingNameWhen overridden in a derived class, gets the human-readable description of the current encoding. (Inherited from Encoding.)
Public propertyHeaderNameWhen overridden in a derived class, gets a name for the current encoding that can be used with mail agent header tags. (Inherited from Encoding.)
Public propertyIsBrowserDisplayWhen overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for displaying content. (Inherited from Encoding.)
Public propertyIsBrowserSaveWhen overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for saving content. (Inherited from Encoding.)
Public propertyIsMailNewsDisplayWhen overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for displaying content. (Inherited from Encoding.)
Public propertyIsMailNewsSaveWhen overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for saving content. (Inherited from Encoding.)
Public propertyIsReadOnlyWhen overridden in a derived class, gets a value indicating whether the current encoding is read-only. (Inherited from Encoding.)
Public propertyIsSingleByteWhen overridden in a derived class, gets a value indicating whether the current encoding uses single-byte code points. (Inherited from Encoding.)
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryWebNameWhen overridden in a derived class, gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. (Inherited from Encoding.)
Public propertyWindowsCodePageWhen overridden in a derived class, gets the Windows operating system code page that most closely corresponds to the current encoding. (Inherited from Encoding.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkCloneWhen overridden in a derived class, creates a shallow copy of the current Encoding object. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsDetermines whether the specified Object is equal to the current UnicodeEncoding object. (Overrides Encoding.Equals(Object).)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetByteCount(Char[])When overridden in a derived class, calculates the number of bytes produced by encoding all the characters in the specified character array. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetByteCount(String)Calculates the number of bytes produced by encoding the characters in the specified string. (Overrides Encoding.GetByteCount(String).)
Public methodSupported by Portable Class LibraryGetByteCount(Char*, Int32)When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. (Inherited from Encoding.)
Public methodGetByteCount(Char*, Int32)Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. (Overrides Encoding.GetByteCount(Char*, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetByteCount(Char[], Int32, Int32)Calculates the number of bytes produced by encoding a set of characters from the specified character array. (Overrides Encoding.GetByteCount(Char[], Int32, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetBytes(Char[])When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetBytes(String)When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetBytes(Char[], Int32, Int32)When overridden in a derived class, encodes a set of characters from the specified character array into a sequence of bytes. (Inherited from Encoding.)
Public methodSupported by Portable Class LibraryGetBytes(Char*, Int32, Byte*, Int32)When overridden in a derived class, encodes a set of characters starting at the specified character pointer into a sequence of bytes that are stored starting at the specified byte pointer. (Inherited from Encoding.)
Public methodGetBytes(Char*, Int32, Byte*, Int32)Encodes a set of characters starting at the specified character pointer into a sequence of bytes that are stored starting at the specified byte pointer. (Overrides Encoding.GetBytes(Char*, Int32, Byte*, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetBytes(Char[], Int32, Int32, Byte[], Int32)Encodes a set of characters from the specified character array into the specified byte array. (Overrides Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetBytes(String, Int32, Int32, Byte[], Int32)Encodes a set of characters from the specified String into the specified byte array. (Overrides Encoding.GetBytes(String, Int32, Int32, Byte[], Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetCharCount(Byte[])When overridden in a derived class, calculates the number of characters produced by decoding all the bytes in the specified byte array. (Inherited from Encoding.)
Public methodSupported by Portable Class LibraryGetCharCount(Byte*, Int32)When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer. (Inherited from Encoding.)
Public methodGetCharCount(Byte*, Int32)Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer. (Overrides Encoding.GetCharCount(Byte*, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetCharCount(Byte[], Int32, Int32)Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. (Overrides Encoding.GetCharCount(Byte[], Int32, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetChars(Byte[])When overridden in a derived class, decodes all the bytes in the specified byte array into a set of characters. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetChars(Byte[], Int32, Int32)When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a set of characters. (Inherited from Encoding.)
Public methodSupported by Portable Class LibraryGetChars(Byte*, Int32, Char*, Int32)When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer. (Inherited from Encoding.)
Public methodGetChars(Byte*, Int32, Char*, Int32)Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer. (Overrides Encoding.GetChars(Byte*, Int32, Char*, Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetChars(Byte[], Int32, Int32, Char[], Int32)Decodes a sequence of bytes from the specified byte array into the specified character array. (Overrides Encoding.GetChars(Byte[], Int32, Int32, Char[], Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetDecoderObtains a decoder that converts a UTF-16 encoded sequence of bytes into a sequence of Unicode characters. (Overrides Encoding.GetDecoder().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetEncoderObtains an encoder that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes. (Overrides Encoding.GetEncoder().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for the current instance. (Overrides Encoding.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetMaxByteCountCalculates the maximum number of bytes produced by encoding the specified number of characters. (Overrides Encoding.GetMaxByteCount(Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetMaxCharCountCalculates the maximum number of characters produced by decoding the specified number of bytes. (Overrides Encoding.GetMaxCharCount(Int32).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetPreambleReturns a Unicode byte order mark encoded in UTF-16 format, if the constructor for this instance requests a byte order mark. (Overrides Encoding.GetPreamble().)
Public methodGetString(Byte[])When overridden in a derived class, decodes all the bytes in the specified byte array into a string. (Inherited from Encoding.)
Public methodGetString(Byte*, Int32)When overridden in a derived class, decodes a specified number of bytes starting at a specified address into a string. (Inherited from Encoding.)
Public methodSupported by the XNA FrameworkGetString(Byte[], Int32, Int32)Decodes a range of bytes from a byte array into a string. (Overrides Encoding.GetString(Byte[], Int32, Int32).)

In XNA Framework 3.0, this member is inherited from Encoding.GetString(Byte[], Int32, Int32).
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsAlwaysNormalized()Gets a value indicating whether the current encoding is always normalized, using the default normalization form. (Inherited from Encoding.)
Public methodIsAlwaysNormalized(NormalizationForm)When overridden in a derived class, gets a value indicating whether the current encoding is always normalized, using the specified normalization form. (Inherited from Encoding.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkCharSizeRepresents the Unicode character size in bytes. This field is a constant.
Top

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.

The Unicode Standard assigns a code point (a number) to each character in every supported script. A Unicode Transformation Format (UTF) is a way to encode that code point. The Unicode Standard uses the following UTFs:

  • UTF-8, which represents each code point as a sequence of one to four bytes.

  • UTF-16, which represents each code point as a sequence of one to two 16-bit integers.

  • UTF-32, which represents each code point as a 32-bit integer.

For more information about the UTFs and other encodings supported by System.Text, see Character Encoding in the .NET Framework.

The UnicodeEncoding class represents a UTF-16 encoding. The encoder can use either big endian byte order (most significant byte first) or 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 00 00 41

  • Little endian byte order: 41 00 00 00

It is generally more efficient to store Unicode characters using the native byte order of a particular platform. For example, it is better to use the little endian byte order on little endian platforms, such as Intel computers. The UnicodeEncoding class corresponds to the Windows code pages 1200 (little endian byte order) and 1201 (big endian byte order). You can determine the "endianness" of a particular architecture by calling the BitConverter.IsLittleEndian method.

Optionally, the UnicodeEncoding object provides a byte order mark (BOM), which 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 (BOM), it helps the decoder determine the byte order and the transformation format or UTF.

If the UnicodeEncoding instance is configured to provide a BOM, you can retrieve it by calling the GetPreamble method; otherwise, the method returns an empty array. Note that, even if a UnicodeEncoding object is configured for BOM support, you must include the BOM at the beginning of the encoded byte stream as appropriate; the encoding methods of the UnicodeEncoding class do not do this automatically.

To enable error detection and to make the class instance more secure, you should instantiate a UnicodeEncoding object by calling the UnicodeEncoding(Boolean, Boolean, Boolean) constructor and setting its throwOnInvalidBytes argument to true. With error detection, a method that detects an invalid sequence of characters or bytes throws a ArgumentException. Without error detection, no exception is thrown, and the invalid sequence is generally ignored.

You can instantiate a UnicodeEncoding object in a number of ways, depending on whether you want to it to provide a byte order mark (BOM), whether you want big-endian or little-endian encoding, and whether you want to enable error detection. The following table lists the UnicodeEncoding constructors and the Encoding properties that return a UnicodeEncoding object.

Member

Endianness

BOM

Error detection

BigEndianUnicode

Big-endian

Yes

No (Replacement fallback)

Encoding.Unicode

Little-endian

Yes

No (Replacement fallback)

UnicodeEncoding.UnicodeEncoding()

Little-endian

Yes

No (Replacement fallback)

UnicodeEncoding(Boolean, Boolean)

Configurable

Configurable

No (Replacement fallback)

UnicodeEncoding.UnicodeEncoding(Boolean, Boolean, Boolean)

Configurable

Configurable

Configurable

The GetByteCount method determines how many bytes result in encoding a set of Unicode characters, and the GetBytes method performs the actual encoding.

Likewise, the GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetChars and GetString methods perform the actual decoding.

For an encoder or decoder that is able to save state information when encoding or decoding data that spans multiple blocks (such as string of 1 million characters that is encoded in 100,000-character segments), use the GetEncoder and GetDecoder properties, respectively.

The following example demonstrates how to encode a string of Unicode characters into a byte array by using a UnicodeEncoding object. The byte array is decoded into a string to demonstrate that there is no loss of data.

using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        // The encoding.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a string that contains Unicode characters.
        String unicodeString =
            "This Unicode string contains two characters " +
            "with codes outside the traditional ASCII code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = unicode.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();

        // Decode bytes back to string. 
        // Notice Pi and Sigma characters are still present.
        String decodedString = unicode.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}

The following example uses the same string as the previous one, except that it writes the encoded bytes to a file and prefixes the byte stream with a byte order mark (BOM). It then reads the file in two different ways: as a text file by using a StreamReader object; and as a binary file. As you would expect, in neither case is the BOM included in the newly read string.

using System;
using System.IO;
using System.Text;

public class Example
{
   public static void Main()
   {
        // Create a UTF-16 encoding that supports a BOM.
        Encoding unicode = new UnicodeEncoding();

        // A Unicode string with two characters outside an 8-bit code range.
        String unicodeString =
            "This Unicode string has 2 characters outside the " +
            "ASCII range: \n" +
            "Pi (\u03A0)), and Sigma (\u03A3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);
        Console.WriteLine();

        // Encode the string.
        Byte[] encodedBytes = unicode.GetBytes(unicodeString);
        Console.WriteLine("The encoded string has {0} bytes.\n",
                          encodedBytes.Length);

        // Write the bytes to a file with a BOM. 
        var fs = new FileStream(@".\UTF8Encoding.txt", FileMode.Create);
        Byte[] bom = unicode.GetPreamble();
        fs.Write(bom, 0, bom.Length);
        fs.Write(encodedBytes, 0, encodedBytes.Length);
        Console.WriteLine("Wrote {0} bytes to the file.\n", fs.Length);
        fs.Close();

        // Open the file using StreamReader. 
        var sr = new StreamReader(@".\UTF8Encoding.txt");
        String newString = sr.ReadToEnd();
        sr.Close();
        Console.WriteLine("String read using StreamReader:");
        Console.WriteLine(newString);
        Console.WriteLine();

        // Open the file as a binary file and decode the bytes back to a string.
        fs = new FileStream(@".\UTF8Encoding.txt", FileMode.Open);
        Byte[] bytes = new Byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        fs.Close();

        String decodedString = unicode.GetString(encodedBytes);
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
   }
}
// The example displays the following output: 
//    Original string: 
//    This Unicode string has 2 characters outside the ASCII range: 
//    Pi (π), and Sigma (Σ). 
// 
//    The encoded string has 172 bytes. 
// 
//    Wrote 174 bytes to the file. 
// 
//    String read using StreamReader: 
//    This Unicode string has 2 characters outside the ASCII range: 
//    Pi (π), and Sigma (Σ). 
// 
//    Decoded bytes: 
//    This Unicode string has 2 characters outside the ASCII range: 
//    Pi (π), and Sigma (Σ).

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft