Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read byte[] with current encoding using streamreader

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

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

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

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

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

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

like image 953
Ori Avatar asked May 16 '13 21:05

Ori


People also ask

How do I get bytes from MemoryStream?

To get the entire buffer, use the GetBuffer method. This method returns a copy of the contents of the MemoryStream as a byte array. If the current instance was constructed on a provided byte array, a copy of the section of the array to which this instance has access is returned.

What does a StreamReader do?

C# StreamReader is used to read characters to a stream in a specified encoding. StreamReader. Read method reads the next character or next set of characters from the input stream. StreamReader is inherited from TextReader that provides methods to read a character, block, line, or all content.


1 Answers

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

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);
like image 196
Jan Dörrenhaus Avatar answered Oct 09 '22 18:10

Jan Dörrenhaus