Get Utf 16 String from byte array - CSharp System

CSharp examples for System:Byte Array

Description

Get Utf 16 String from byte array

Demo Code


using System;/*  w w  w.  j  ava2s .  c  o  m*/

public class Main{
        static public string GetUtf16String(this byte[] n, UInt32 aStart, UInt32 aCharCount)
        {
            //TODO: This routine only handles ASCII. It does not handle unicode yet.
            var xChars = new char[aCharCount];
            for (int i = 0; i < aCharCount; i++)
            {
                UInt32 xPos = (UInt32)(aStart + i * 2);
                var xChar = (UInt16)(n[xPos + 1] << 8 | n[xPos]);
                if (xChar == 0)
                {
                    return new string(xChars, 0, i);
                }
                xChars[i] = (char)xChar;
            }
            return new string(xChars);
        }
}

Related Tutorials