Example

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.

using System;
using System.Text;
namespace Samples
{
  public class DecoderSample
  {
    public static void Main()
    {
      byte[] b1 = { 0x41, 0x23, 0xe2 };
      byte[] b2 = { 0x98, 0xa3 };
      Decoder d = Encoding.UTF8.GetDecoder();
      char[] chars = new char[3];
      int i = d.GetChars(b1, 0, b1.Length, chars, 0);
      i += d.GetChars(b2, 0, b2.Length, chars, i);
      foreach(char c in chars)
        Console.Write("U+{0:x} ", (ushort)c);
    }
  }
}
The output is
U+41 U+23 U+2623

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.