Encoder converts a set of characters into a sequence of bytes.
using System; using System.Text; class EncoderTest { public static void Main() { Char[] chars = new Char[] {'\u03a3' // Sigma }; Encoding encoding = Encoding.UTF7; Byte[] allCharactersFromEncoding = encoding.GetBytes(chars); Console.WriteLine("All characters encoded:"); ShowArray(allCharactersFromEncoding); Encoder encoder = new UnicodeEncoding().GetEncoder(); Byte[] firstchar = encoding.GetBytes(chars, 0, 1); Console.WriteLine("First character:"); ShowArray(firstchar); Byte[] allCharactersFromEncoder = new Byte[encoder.GetByteCount(chars, 0, chars.Length, true)]; encoder.GetBytes(chars, 0, chars.Length, allCharactersFromEncoder, 0, true); Console.WriteLine("All characters encoded:"); ShowArray(allCharactersFromEncoder); bool bFlushState = false; Byte[] firstcharNoFlush = new Byte[encoder.GetByteCount(chars, 0, 1, bFlushState)]; encoder.GetBytes(chars, 0, 1, firstcharNoFlush, 0, bFlushState); ShowArray(firstcharNoFlush); } public static void ShowArray(Array theArray) { foreach (Object o in theArray) { Console.Write("[{0}]", o); } Console.WriteLine("\n"); } }
1. | Create a new instance of the Encoder class | ||
2. | Calculates the number of bytes produced by encoding a set of characters | ||
3. | Encode a set of characters from the specified character array |