public abstract class Encoder
Object
Encoder
mscorlib
BCL
Converts blocks of characters into blocks of bytes.
[Note: Following instantiation of a Encoder, sequential blocks of characters are converted into blocks of bytes through calls to the System.Text.Encoder.GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32,System.Boolean) 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 Encoder class is typically obtained through a call to the System.Text.Encoding.GetEncoder .]
The following example demonstrates using the UTF8Encoding implementation of the Encoder class to convert one character array to two byte arrays.
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'
System.Text Namespace
Encoder Constructors
Encoder Methods
protected Encoder();
Constructs a new instance of the Encoder class.
This constructor is called only by classes that inherit from the Encoder class.
System.Text.Encoder Class, System.Text Namespace
public abstract int GetByteCount(char[] chars, int index, int count, bool flush);
Determines the exact number of bytes required to encode the specified range in the specified array of characters.
- chars
- A Char array of characters to encode.
- index
- A Int32 that specifies the first index of chars to encode.
- count
- A Int32 that specifies the number of elements in chars to encode.
- flush
- A Boolean value that determines whether the current instance flushes its internal state following a conversion. Specify
true
to flush the internal state of the current instance following a conversion; otherwise, specifyfalse
.
A Int32 containing the number of bytes required to encode the range in chars from index to index + count for a particular encoding.[Note: This value takes into account the state in which the current instance was left following the last call to System.Text.Encoder.GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32,System.Boolean) .]
Exception Type Condition ArgumentNullException chars is null
.ArgumentOutOfRangeException Return value is greater than System.Int32.MaxValue. -or-
index < 0.
-or-
count < 0.
-or-
index and count do not specify a valid range in chars (i.e. (index + count) > chars.Length).
The state of the current instance is not affected by a call to this method.[Behaviors: As described above.]
[Overrides: Override this method to retrieve the exact number of bytes required to encode a specified range of an array of Char objects for a particular encoding.]
[Usage: Use this method to determine the exact number of bytes required to encode the specified range of an array of Char objects for a particular encoding.]
System.Text.Encoder Class, System.Text Namespace
public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush);
Encodes the specified range of the specified array of characters into the specified range of the specified array of bytes.
- chars
- A Char array of characters to encode.
- charIndex
- A Int32 that specifies the first index of chars to encode.
- charCount
- A Int32 that specifies the number of elements in chars to encode.
- bytes
- A Byte array to encode into.
- byteIndex
- A Int32 that specifies the first index of bytes to encode into.
- flush
- A Boolean value. Specify
true
to flush the internal state of the current instance following a conversion; otherwise, specifyfalse
. [Note: To ensure correct termination of a sequence of blocks of encoded bytes, it is recommended that the last call to System.Text.Encoder.GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32,System.Boolean) specifytrue
.]
A Int32 containing the number of bytes encoded into bytes for a particular encoding.
Exception Type Condition ArgumentException bytes does not contain sufficient space to store the encoded characters.
ArgumentNullException chars is null
.-or-
bytes is
null
.
ArgumentOutOfRangeException charIndex < 0. -or-
charCount < 0.
-or-
byteIndex < 0.
-or-
(chars.Length - charIndex) < charCount.
-or-
byteIndex > bytes.Length.
The encoding takes into account the state in which the current instance was left following the last call to this method if flush was specified astrue
for that call.[Behaviors: As described above.]
[Overrides: Override this method to encode the values of an array of Char objects as an array of Byte objects for a particular encoding.]
[Usage: Use this method to encode the values of an array of Char objects as an array of Byte objects for a particular encoding.]
System.Text.Encoder Class, System.Text Namespace