Encoding.UTF8.GetString doesn't take into account the Preamble/BOM

后端 未结 4 754
感情败类
感情败类 2020-11-28 13:26

In .NET, I\'m trying to use Encoding.UTF8.GetString method, which takes a byte array and converts it to a string.

It looks like this method

4条回答
  •  情歌与酒
    2020-11-28 14:02

    Based on the answer by Jon Skeet (thanks!), this is how I just did it:

    var memoryStream = new MemoryStream(byteArray);
    var s = new StreamReader(memoryStream).ReadToEnd();
    

    Note that this will probably only work reliably if there is a BOM in the byte array you are reading from. If not, you might want to look into another StreamReader constructor overload which takes an Encoding parameter so you can tell it what the byte array contains.

提交回复
热议问题