Problem with encoding....

Z

ZoombyWoof

Hi all. I have a c# app that sometimes writes to a textfile. Sometimes
the text contains the swedish letters åäö. In the app these letters look
ok, but when StreamWriter has written them to the file they look like
shit. Some encoding mismatch is happening here I suspect, but I cant
find it.

I run English version of XP with swedish regional settings everywhere I
can find them :).

Any hints would be much appreciated. Thanx

/ZW
 
Z

ZoombyWoof

Hi. Thanx :) I had to specify the encoding in the call to the
StreamWriter constructor and this one worked:
sw = new StreamWriter( fullname, false, System.Text.Encoding.Unicode );

The file gets twice the size as before, but it works.

Thanx again

/ZW
 
A

AlexS

Hi, ZoombyWoof

Stream uses default Encodings and you might need to specify directly which
one to use

e.g. for encoding bytes from file properly to string I use constructs like
System.Text.Encoding.GetEncoding(myEncodingNumber).GetString(data)

You might need to use similar approach when preparing data for StreamWriter

HTH
Alex
 
J

Jon Skeet [C# MVP]

ZoombyWoof said:
Hi. Thanx :) I had to specify the encoding in the call to the
StreamWriter constructor and this one worked:
sw = new StreamWriter( fullname, false, System.Text.Encoding.Unicode );

The file gets twice the size as before, but it works.

If you're able to specify the encoding for both writing and reading,
I'd recommend Encoding.UTF8, which is very efficient for mainly-ASCII
files and can still represent all Unicode characters.

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

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