C# UnicodeEncoding GetEncoder
Description
UnicodeEncoding GetEncoder
Obtains an encoder that
converts a sequence of Unicode characters into a UTF-16 encoded sequence
of bytes.
Syntax
UnicodeEncoding.GetEncoder
has the following syntax.
[ComVisibleAttribute(false)]
public override Encoder GetEncoder()
Returns
UnicodeEncoding.GetEncoder
method returns A Encoder object that converts a sequence of Unicode characters into a UTF-16
encoded sequence of bytes.
Example
using System;/* w w w .jav a 2 s . c o m*/
using System.Text;
public class SamplesUnicodeEncoding {
public static void Main() {
UnicodeEncoding u16 = new UnicodeEncoding( false, true, true );
Encoder myEnc = u16.GetEncoder();
Decoder myDec = u16.GetDecoder();
char[] myChars = new char[5] { 'z', 'a', 'd','e','f'};
int iBC = myEnc.GetByteCount( myChars, 0, myChars.Length, true );
byte[] myBytes = new byte[iBC];
myEnc.GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );
Console.Write( "Using the encoder : " );
for ( int i = 0; i < myBytes.Length; i++ )
Console.Write( "{0:X2} ", myBytes[i] );
int iCC = myDec.GetCharCount( myBytes, 0, myBytes.Length, true );
char[] myDecodedChars = new char[iCC];
myDec.GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );
Console.WriteLine( myDecodedChars );
}
}
The code above generates the following result.