This documentation is archived and is not being maintained.

UTF32Encoding Class

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

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.

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

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
Show: