Any sample code to test the encoding of a text file?

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hi,

Any sample code which test the encoding of a text file?

Most results I found in google is : unless you read the file, you will
never know the encoding,

but the problem is I need to tell the streamreader the encoding to be
read, how to do it?

Thanks.

Nick
 
Nick said:
Any sample code which test the encoding of a text file?

Most results I found in google is : unless you read the file, you will
never know the encoding,

Well that's *certainly* true.
but the problem is I need to tell the streamreader the encoding to be
read, how to do it?

If you're going to have to read the data first, you might end up
wanting to avoid StreamReader entirely, unless you're going to read it,
then open the file again for reading with StreamReader.

You might want to write your own class which reads the file (or at
least part of it) in binary, analyses the data, and then converts it to
text itself based on whatever encoding it thinks is appropriate. Just
don't expect it to get it right 100% of the time.
 
Hi Nick,

There is another option. Read the file. If, after you have read the file, find out that it is in another format, convert the file from one encoding to another. Useful when network streams are involved.
 
No, I mean read the file as UTF-8 or whatever is the most likely encoding. I used this for downloading web pages that didn't properly specify the encoding used. If they somewhere in the text did specify the encoding I just converted the entire text (now already in memory) to the new encoding.

But you could read it as a binary and store it in a byte array until you can figure out what the data really is encoded in.
 
Back
Top