Export (0) Print
Expand All

UTF32Encoding Class

Note: This class is new in the .NET Framework version 2.0.

Represents a UTF-32 encoding of Unicode characters.

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

'Declaration
<SerializableAttribute> _
Public NotInheritable Class UTF32Encoding
	Inherits Encoding
'Usage
Dim instance As UTF32Encoding

/** @attribute SerializableAttribute() */ 
public final class UTF32Encoding extends Encoding
SerializableAttribute 
public final class UTF32Encoding extends Encoding

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the reverse; it 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 version 3.2 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.

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.

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

Optionally, the UTF32Encoding 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: 00 00 FE FF

  • Little-endian byte order: FF FE 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 machines.

The GetPreamble method returns an array of bytes containing the byte order mark. If this byte array is prefixed to an encoded stream, it helps the decoder to identify the encoding format used.

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

NoteNote

To enable error detection and to make the class instance more secure, 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 an ArgumentException. Without error detection, no exception is thrown, and the invalid sequence is generally ignored.

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

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

Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Public Class SamplesUTF32Encoding   

   Public Shared Sub Main()

      ' Create an instance of UTF32Encoding using little-endian byte order.
      ' This will be used for encoding.
      Dim u32LE As 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.
      Dim u32withED As New UTF32Encoding(True, True, True)
      Dim u32noED As 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)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)

      ' Encode the string using little-endian byte order.
      Dim myBytes(u32LE.GetByteCount(myStr)) As Byte
      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)

   End Sub 'Main


   ' Decode the bytes and display the string.
   Public Shared Sub PrintDecodedString(bytes() As Byte, enc As Encoding)

      Try
         Console.WriteLine("   Decoded string: {0}", enc.GetString(bytes, 0, bytes.Length))
      Catch e As System.ArgumentException
         Console.WriteLine(e.ToString())
      End Try

      Console.WriteLine()

   End Sub 'PrintDecodedString 

End Class 'SamplesUTF32Encoding


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

public class SamplesUTF32Encoding
{
    public static void main(String[] args)
    {
        // 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.
        ubyte myBytes[] = new ubyte[u32LE.GetByteCount(myStr)];
        u32LE.GetBytes(myStr, 0, myStr.get_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);
    } //main

    // Decode the bytes and display the string.
    public static void PrintDecodedString(ubyte 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();
    } //PrintDecodedString
} //SamplesUTF32Encoding

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 98, Windows 2000 SP4, Windows Millennium Edition, 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

Community Additions

ADD
Show:
© 2014 Microsoft