Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

UTF7Encoding Class

Represents a UTF-7 encoding of Unicode characters.

For a list of all members of this type, see UTF7Encoding Members.

System.Object
   System.Text.Encoding
      System.Text.UTF7Encoding

[Visual Basic]
<Serializable>
Public Class UTF7Encoding
   Inherits Encoding
[C#]
[Serializable]
public class UTF7Encoding : Encoding
[C++]
[Serializable]
public __gc class UTF7Encoding : public Encoding
[JScript]
public
   Serializable
class UTF7Encoding extends Encoding

Thread Safety

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

Remarks

This class encodes Unicode characters using UCS Transformation Format, 7-bit form (UTF-7). This encoding supports all Unicode character values. Surrogate pairs are encoded as two separate surrogate code points.

UTF-7 encoding was originally invented to efficiently transmit Unicode characters through e-mail systems optimized for US-ASCII text messages.

This class contains the GetCharCount method that reports the number of Unicode characters that result from decoding an array of bytes, and the GetChars method that actually decodes an array of bytes. The GetByteCount method reports the number of bytes that result from encoding arrays of Unicode characters, and the GetBytes method actually encodes characters into an array of bytes.

The GetDecoder method obtains an object to convert (decode) UTF-7 encoded bytes into Unicode characters, whereas the GetEncoder method obtains an object to convert (encode) Unicode characters into UTF-7 encoded bytes.

This class inherits from the Encoding class.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use a UTF7Encoding to encode a string of Unicode characters and store them in the byte array encodedBytes. Notice that when encodedBytes is decoded back to a string there is no loss of data.

[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        ' Create a UTF-7 encoding.
        Dim utf7 As New UTF7Encoding()
        
        ' A Unicode string with two characters outside a 7-bit code range.
        Dim unicodeString As String = _
            "This Unicode string contains two characters " & _
            "with codes outside a 7-bit code range, " & _
            "Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")."
        Console.WriteLine("Original string:")
        Console.WriteLine(unicodeString)
        
        ' Encode the string.
        Dim encodedBytes As Byte() = utf7.GetBytes(unicodeString)
        Console.WriteLine()
        Console.WriteLine("Encoded bytes:")
        Dim b As Byte
        For Each b In  encodedBytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
        
        ' Decode bytes back to string.
        ' Notice Pi and Sigma characters are still present.
        Dim decodedString As String = utf7.GetString(encodedBytes)
        Console.WriteLine()
        Console.WriteLine("Decoded bytes:")
        Console.WriteLine(decodedString)
    End Sub
End Class

[C#] 
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        // Create a UTF-7 encoding.
        UTF7Encoding utf7 = new UTF7Encoding();
        
        // A Unicode string with two characters outside a 7-bit code range.
        String unicodeString =
            "This Unicode string contains two characters " +
            "with codes outside a 7-bit code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = utf7.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 = utf7.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main()
{
   // Create a UTF-7 encoding.
   UTF7Encoding* utf7 = new UTF7Encoding();

   // A Unicode string with two characters outside a 7-bit code range.
   String * unicodeString =
      S"This Unicode string contains two characters with codes outside a 7-bit code range, Pi (\u03a0) and Sigma (\u03a3).";
   Console::WriteLine(S"Original string:");
   Console::WriteLine(unicodeString);

   // Encode the string.
   Byte encodedBytes[] = utf7 -> GetBytes(unicodeString);
   Console::WriteLine();
   Console::WriteLine(S"Encoded bytes:");
   IEnumerator* myEnum = encodedBytes->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
      Console::Write(S"[{0}]", __box(b));
   }

   Console::WriteLine();

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Text

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

UTF7Encoding Members | System.Text Namespace | Decoder | Encoder

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft