Problem with encoding....

  • Thread starter Thread starter ZoombyWoof
  • Start date Start date
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
 
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
 
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
 
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.
 
Back
Top