How to write plain ascii to a textfile

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I think I overlook something, but when a write to a text file using a
StreamWriter, the file is written as unicode. I'm writing information that is
retrieve from nvarchar fields on SQL server. I have a problem with characters
like Ü, Ö, etc. When looking at the file they seems to be correct, but in
some applications the characters are shown as Ü or Ö. How can I write
single byte characters to the file? Just specifying new
StreamWriter(FileName, false, Encoding.ASCII) doesn't solve the problem,
because the characters are completely wrong. Please advise.

Thanks in advance.
 
Use Encoding.Default to use the current ANSI code page of your computer:

StreamWriter OutFile = new StreamWriter("c:\\temp\\filename.txt",
Encoding.Default).

Lionel.
 
Well are you trying to write unicode or ASCII? You say you want to write
ASCII but have non-ASCII characters handled properly?

Anyway, it sounds more like the problem is on the other end, that the "some
applications" are not handling the data correctly. If they require ASCII,
then stop trying to send them non-ASCII characters.
 
Edward said:
I think I overlook something, but when a write to a text file using a
StreamWriter, the file is written as unicode.

By default it's written as UTF-8.
I'm writing information that is
retrieve from nvarchar fields on SQL server. I have a problem with characters
like ?, ?, etc. When looking at the file they seems to be correct, but in
some applications the characters are shown as ?? or ??. How can I write
single byte characters to the file? Just specifying new
StreamWriter(FileName, false, Encoding.ASCII) doesn't solve the problem,
because the characters are completely wrong. Please advise.

I assume those question marks are meant to have non-ASCII characters -
eg accented characters. Note that ASCII doesn't have any accents - what
people often think of as "extended ASCII" is a variety of 8-bit
character sets which are compatible with ASCII for the first 128
values.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
All the stream wrier and reader classes use UTF8 encoding for text input and
output. If your source string is unicode or mutibyte, then it is going to
read it as multibyte. You will have to change the encoding of the writer.
 
Thanks,

I knew it was UTF-8 and noticed that indeed the accented characters were
written as double byte. How can I write it as a single byte? For example the
capital A with two dots above converted to ascii-code 142.
 
Edward said:
I knew it was UTF-8 and noticed that indeed the accented characters were
written as double byte. How can I write it as a single byte? For example the
capital A with two dots above converted to ascii-code 142.

There's no such thing as ascii-code 142. ASCII only goes as far as 127
(or 126, depending on how you count it). I suspect you mean either
ISO-8859-1 or Windows Code Page 1252. Please read the page I linked to
before (http://www.pobox.com/~skeet/csharp/unicode.html) which has more
information about the encodings and how to use them from .NET.
 
Lionel LASKE said:
Use Encoding.Default to use the current ANSI code page of your computer:

StreamWriter OutFile = new StreamWriter("c:\\temp\\filename.txt",
Encoding.Default).

Lionel.

Edward, it really is as simple as this... =)

- Michael S
 
Back
Top