UTF7Encoding.GetEncoder Method ()

 

Obtains an encoder that converts a sequence of Unicode characters into a UTF-7 encoded sequence of bytes.

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

public override Encoder GetEncoder()

Return Value

Type: System.Text.Encoder

A Encoder that converts a sequence of Unicode characters into a UTF-7 encoded sequence of bytes.

The Decoder.GetChars method converts sequential blocks of bytes into sequential blocks of characters, in a manner similar to the GetChars method. However, a Decoder maintains state information between calls so it can correctly decode byte sequences that span blocks. The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

The following code example demonstrates how to use the GetEncoder method to obtain an encoder to convert a sequence of characters into a UTF-7 encoded sequence of bytes.

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        Char[] chars = new Char[] {'a', 'b', 'c', '\u0300', '\ua0a0'};
        Byte[] bytes;

        Encoder utf7Encoder = Encoding.UTF7.GetEncoder();

        int byteCount = utf7Encoder.GetByteCount(chars, 2, 3, true);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7Encoder.GetBytes(chars, 2, 3, bytes, 0, true);

        Console.WriteLine(
            "{0} bytes used to encode characters.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show: