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

UTF32Encoding Class

Represents a UTF-32 encoding of Unicode characters.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
public sealed class UTF32Encoding : Encoding

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 UTF-32 encoding represents each code point as a 32-bit integer. For more information about the UTFs and other encodings supported by System.Text, see Understanding Encodings and Using Unicode Encoding.

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.

UTF32Encoding corresponds to the Windows code pages 12000 (little endian byte order) and 12001 (big endian byte order).

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 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. For example, it is better to use the little endian byte order on little endian platforms, such as Intel computers.

The GetPreamble method retrieves an array of bytes that can include the byte order mark (BOM). If this byte array is prefixed to an encoded stream, it helps the decoder to identify the encoding format used.

For more information on byte order and the byte order mark, see The Unicode Standard at the Unicode home page.

NoteNote:

To enable error detection and to make the class instance more secure, the application should use the UTF32Encoding constructor that takes a throwOnInvalidCharacters parameter, and set that parameter 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.

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

using System;
using System.Text;

public class SamplesUTF32Encoding  {

   public static void Main()  {

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

      // Create two instances of UTF32Encoding using big-endian byte order: one with error detection and one without. 
      // These will be used for decoding.
      UTF32Encoding u32withED = new UTF32Encoding( true, true, true );
      UTF32Encoding u32noED   = new UTF32Encoding( 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) 
      //    a high-surrogate value (U+D8FF) 
      //    a low-surrogate value (U+DCFF)
      String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";

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

      // Decode the byte array with error detection.
      Console.WriteLine( "Decoding with error detection:" );
      PrintDecodedString( myBytes, u32withED );

      // Decode the byte array without error detection.
      Console.WriteLine( "Decoding without error detection:" );
      PrintDecodedString( myBytes, u32noED );

   }


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

      try  {
         Console.WriteLine( "   Decoded string: {0}", enc.GetString( bytes, 0, bytes.Length ) );
      }
      catch ( System.ArgumentException e )  {
         Console.WriteLine( e.ToString() );
      }

      Console.WriteLine();

   }

}
System.Object
  System.Text.Encoding
    System.Text.UTF32Encoding
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.