Mono Class Library: System.Text Namespace

System.Text.Encoder Class

Converts blocks of characters into blocks of bytes. [Edit]

See Also: Encoder Members

[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoder

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Note: Following instantiation of a System.Text.Encoder, sequential blocks of characters are converted into blocks of bytes through calls to the Encoder.GetBytes(Char[], int, int, Byte[], int, bool) method. The encoder maintains state between the conversions, allowing it to correctly encode character sequences that span adjacent blocks. An instance of a specific implementation of the System.Text.Encoder class is typically obtained through a call to the Encoding.GetEncoder .
[Edit]

Example

The following example demonstrates using the System.Text.UTF8Encoding class to convert one character array to two byte arrays.

C# Example
using System;
using System.Text;

public class EncoderExample
{

   public static void Main()
   {

      string str = "Encoder";
      char[] cAry = str.ToCharArray();
      UTF8Encoding utf = new UTF8Encoding();
    
      Encoder e = utf.GetEncoder();
      int count1 = 
         e.GetByteCount(cAry,0,cAry.Length-4,false);
      int count2 = 
         e.GetByteCount(cAry,cAry.Length-4,4,true);
      byte[] bytes1 = new byte[count1];
      byte[] bytes2 = new byte[count2];
      
      e.GetBytes(cAry,0,cAry.Length-4,bytes1,0,false);
      e.GetBytes(cAry,cAry.Length-4,4,bytes2,0,true);

      Console.Write("Bytes1: ");
      foreach (byte b in bytes1)
         Console.Write(" '{0}' ", b);
      Console.WriteLine();

      Console.Write("Bytes2: ");
      foreach (byte b in bytes2)
         Console.Write(" '{0}' ", b);
      Console.WriteLine();
            
   }

}

The output is

Bytes1: '69' '110' '99'
Bytes2: '111' '100' '101' '114'

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0