System.Text.Encoder Class

Converts a set of characters into a sequence of bytes.

See Also: Encoder Members

Syntax

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

Remarks

To obtain an instance of an implementation of the System.Text.Encoder class, the application should use the Encoding.GetEncoder method of an System.Text.Encoding implementation.

The Encoder.GetByteCount(Char*, int, bool) method determines how many bytes result in encoding a set of Unicode characters, and the Encoder.GetBytes(Char*, int, Byte*, int, bool) method performs the actual encoding. There are several versions of both of these methods available in the System.Text.Encoder class. For more information, see Encoding.GetBytes(Char*, int, Byte*, int).

A System.Text.Encoder object maintains state information between successive calls to GetBytes or Decoder.Convert(Byte[], int, int, Char[], int, int, bool, Int32@, Int32@, Boolean@) methods so that it can correctly encode character sequences that span blocks. The System.Text.Encoder also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. Therefore, Encoding.GetDecoder and Encoding.GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

Note:

When the application is done with a stream of data it should make sure that the state information is flushed by setting the flush parameter to true in the appropriate method call. If an exception occurs or if the application switches streams, it should call Encoder.Reset to clear the internal state of the Encoder object.

Version Considerations

A System.Text.Decoder or System.Text.Encoder object can be serialized during a conversion operation. The state of the object is retained if it is deserialized in the same version of the .NET Framework, but lost if it is deserialized in another version.

Thread Safety

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

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, 4.0.0.0