See Also: Decoder Members
The following example demonstrates using the System.Text.UTF8Encoding implementation of the System.Text.Decoder class to convert two byte arrays to a character array, where one character's bytes span multiple byte arrays. This demonstrates how to use a System.Text.Decoder in streaming-like situations.
C# Example
using System;
using System.Text;
public class DecoderExample
{
public static void Main()
{
// These bytes in UTF-8 correspond to 3 different
// Unicode characters - A (U+0041), # (U+0023),
// and the biohazard symbol (U+2623). Note the
// biohazard symbol requires 3 bytes in UTF-8
// (in hex, e2, 98, a3). Decoders store state across
// multiple calls to GetChars, handling the case
// when one char spans multiple byte arrays.
byte[] bytes1 = { 0x41, 0x23, 0xe2 };
byte[] bytes2 = { 0x98, 0xa3 };
char[] chars = new char[3];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(bytes1, 0, bytes1.Length,
chars, 0);
// charLen is 2.
charLen += d.GetChars(bytes2, 0, bytes2.Length,
chars, charLen);
// charLen is now 3.
foreach(char c in chars)
Console.Write("U+{0:x} ", (ushort)c);
}
}
The output is
U+41 U+23 U+2623