Search code examples
.netstringutf8-decode

Convert Byte Array to Text With Byte Order Mark Detection


Trying to figure out the byte[] equivalent of this

    string getText = string.Empty;
    using (StreamReader sr = new StreamReader((System.IO.Stream)File.OpenRead(unc), true))
    {
        getText = sr.ReadToEnd();
    }

Would like to convert a byte[] to string with specified byte order mark detection option like is available with StreamReader.


Solution

  • You can still use StreamReader - just wrap the byte array in a MemoryStream. (It's not clear why you're casting File.OpenRead to Stream, by the way...)

    using (StreamReader sr = new StreamReader(new MemoryStream(data)))