Character encoding problem?

C

CMan

Hi,

I am reading a text file using a StreamReader in C# but the reader is unable
to handle some of the characheters.

Using the default encoding the program cannot handle accented characters. I
tried opening the file using other encodings e.g. UTF7.
UTF7 fixed the accents but cannot hadle the plus sign x2B. I am also having
problems with the Euro symbol x80 and quote x92.

How do I read this file correctly?
Is this problem caused by the encoding?
Is there a way to determine the file's encoding at runtime?
How else can I find out the encoding?

Thanks

Colin
 
J

Jon Skeet [C# MVP]

CMan said:
I am reading a text file using a StreamReader in C# but the reader is unable
to handle some of the characheters.

Using the default encoding the program cannot handle accented characters. I
tried opening the file using other encodings e.g. UTF7.
UTF7 fixed the accents but cannot hadle the plus sign x2B. I am also having
problems with the Euro symbol x80 and quote x92.

If your file is using 0x80 for the Euro symbol, that should help to
narrow it down...

Have you tried using Encoding.Default? That's not the same as the
default encoding for StreamReader when you don't specify an encoding.
(It's the default encoding for your computer, instead.)
How do I read this file correctly?

By specifying the correct encoding.
Is this problem caused by the encoding?

Almost certainly.
Is there a way to determine the file's encoding at runtime?

There are ways you can try to guess it heuristically, but nothing
foolproof.
How else can I find out the encoding?

Well, what generated this text file to start with?

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 
C

CMan

Thanks Jon.

You got in one. Encoding.Default fixed on my my machine.

Should have spotted that one.

Colin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top