如何阅读byte []的使用StreamReader的当前编码byte、StreamReader

2023-09-05 03:58:08 作者:君可知故人盼君归

我想读字节[] 使用C#与文件的当前编码。

I would like to read byte[] using C# with the current encoding of the file.

书面MSDN中的默认编码将是UTF-8,当构造函数没有编码:

As written in MSDN the default encoding will be UTF-8 when the constructor has no encoding:

var reader = new StreamReader(new MemoryStream(data)).

我也试过,但仍然得到的文件为UTF-8:

I have also tried this, but still get the file as UTF-8:

var reader = new StreamReader(new MemoryStream(data),true)

我需要阅读字节[] 与当前的编码。

推荐答案

一个文件没有编码。一个字节数组没有编码。字节没有编码。编码是什么,字节转换为文本,反之亦然。

A file has no encoding. A byte array has no encoding. A byte has no encoding. Encoding is something that transforms bytes to text and vice versa.

您在文本编辑器看到什么之类的其实是计划魔法:编辑尝试了不同的编码的话猜测哪一个是最有意义的。这也是你能够与布尔参数是什么。如果这不会产生你想要什么,那么这个神奇的失败。

What you see in text editors and the like is actually program magic: The editor tries out different encodings an then "guesses" which one makes the most sense. This is also what you enable with the boolean parameter. If this does not produce what you want, then this magic fails.

var reader = new StreamReader(new MemoryStream(data), Encoding.Default);

将使用OS /地区特定的默认编码。如果这还不够,你想要什么,那么你需要完全明确,并告诉StreamReader的使用有什么具体的编码,例如(只是举个例子,你说你不想UTF8):

will use the OS/Location specific default encoding. If that is still not what you want, then you need to be completely explicit, and tell the streamreader what exact encoding to use, for example (just as an example, you said you did not want UTF8):

var reader = new StreamReader(new MemoryStream(data), Encoding.UTF8);