Decode a sequence of bytes from the specified byte array : Decoder « Development Class « C# / C Sharp






Decode a sequence of bytes from the specified byte array

 

using System;
using System.Text;

class MainClass {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        Decoder uniDecoder = Encoding.Unicode.GetDecoder();

        int charCount = uniDecoder.GetCharCount(bytes, 0, bytes.Length);
        chars = new Char[charCount];
        int charsDecodedCount = uniDecoder.GetChars(bytes, 0, bytes.Length, chars, 0);

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);
        }
    }
}

   
  








Related examples in the same category

1.Converts a sequence of encoded bytes into a set of characters.
2.Create a new instance of the Decoder class.
3.Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.