C# UTF8Encoding GetEncoder

Description

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

Syntax

UTF8Encoding.GetEncoder has the following syntax.


public override Encoder GetEncoder()

Returns

UTF8Encoding.GetEncoder method returns A Encoder that converts a sequence of Unicode characters into a UTF-8 encoded sequence of bytes.

Example

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


using System;//from   w w  w.  j  a  va 2s  . c o  m
using System.Text;

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

        Encoder utf8Encoder = Encoding.UTF8.GetEncoder();

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

        Console.WriteLine(bytesEncodedCount);

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding