C# 如何使用streamreader以当前编码读取字节[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16598021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:25:28  来源:igfitidea点击:

How to read byte[] with current encoding using streamreader

c#.netencodingstreamstreamreader

提问by Ori

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

我想byte[]使用 C#阅读文件的当前编码。

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

正如 MSDN 中所写,当构造函数没有编码时,默认编码将为 UTF-8:

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

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

我也试过这个,但仍然以 UTF-8 格式获取文件:

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

I need to read the byte[]with the current encoding.

我需要byte[]使用当前编码阅读。

采纳答案by Jan Doerrenhaus

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 guesseswhich 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.

您在文本编辑器等中看到的实际上是程序魔术:编辑器尝试不同的编码,然后猜测哪种编码最有意义。这也是您使用 boolean 参数启用的功能。如果这不能产生你想要的结果,那么这个魔法就会失败。

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

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):

将使用操作系统/位置特定的默认编码。如果这仍然不是你想要的,那么你需要完全明确,并告诉流阅读器使用什么确切的编码,例如(作为一个例子,你说你不想要 UTF8):

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

回答by Ishwar Nataraj

I just tried leveraging different way of trying to figure out the ByteEncoding and it is not possible to do so as the byte array does not have an encoding in place as Jan mentions in his reply. However you can always take the value and do the type conversion to UTF8 or ASCII/Unicode and test the string values in case you are doing a "Text.EncodingFormat.GetString(byte [] array)"

我只是尝试利用不同的方式来尝试找出 ByteEncoding 并且这是不可能的,因为字节数组没有像 Jan 在他的回复中提到的那样编码。但是,您始终可以使用该值并将类型转换为 UTF8 或 ASCII/Unicode 并测试字符串值,以防您执行“Text.EncodingFormat.GetString(byte [] array)”

public static bool IsUnicode(string input)    
{    
    var asciiBytesCount = Encoding.ASCII.GetByteCount(input);
    var unicodBytesCount = Encoding.UTF8.GetByteCount(input);
    return asciiBytesCount != unicodBytesCount;
}