By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

ASCII, StreamWriter and swedish letters

P: n/a
MA
Hi all!

I have a major problem. I need to write an textfile with 1 b per letter. But
it should be able to handle swedish letters to ().
Is it possible to use 8 b ASCII for this?
This file is used by an sms application and cannot be in another format.

/Marre
Nov 16 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
MA <ne**@supremelink.se> wrote:
I have a major problem. I need to write an textfile with 1 b per letter. But
it should be able to handle swedish letters to ().
Is it possible to use 8 b ASCII for this?
There's no such thing as "8 bit ASCII" (assuming that's what you meant
by "b").
This file is used by an sms application and cannot be in another
format.


You need to find out *exactly* what encoding will be used. There are
various 8 bit character sets which are compatible with ASCII in the
range 0-127, but which are incompatible with each other above 127. If
you can find out which of those your app needs to output, it should be
easy to find the appropriate Encoding to give to your StreamWriter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Hi Marre,

I believe Swedish uses the standard European characterset ISO-8859-1, or you could use ISO-8859-15 which is the nordic set (no and in 8859-1, so I would recomment the latter)

System.IO.StreamWriter sw = new System.IO.StreamWriter(path);
sw.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-15");

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

P: n/a
You could also use cp1252, which supports Swedish well.

Or even better Unicode, which supports everything.
--
MichKa [MS]
NLS Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies
Windows International Division

This posting is provided "AS IS" with
no warranties, and confers no rights.
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opseqzkntdklbvpo@server72...
Hi Marre,

I believe Swedish uses the standard European characterset ISO-8859-1, or you could use ISO-8859-15 which is the nordic set (no and in 8859-1, so
I would recomment the latter)
System.IO.StreamWriter sw = new System.IO.StreamWriter(path);
sw.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-15");

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

P: n/a
On Wed, 22 Sep 2004 07:24:27 -0700, Michael (michka) Kaplan [MS] <mi*****@online.microsoft.com> wrote:
You could also use cp1252, which supports Swedish well.

Or even better Unicode, which supports everything.


Well, Unicode wouldn't be 8-bit, now would it :P

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

P: n/a
UTF-8 works with bytes....
--
MichKa [MS]
NLS Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies
Windows International Division

This posting is provided "AS IS" with
no warranties, and confers no rights.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opseqfx7h3klbvpo@stone...
On Wed, 22 Sep 2004 07:24:27 -0700, Michael (michka) Kaplan [MS] <mi*****@online.microsoft.com> wrote:
You could also use cp1252, which supports Swedish well.

Or even better Unicode, which supports everything.


Well, Unicode wouldn't be 8-bit, now would it :P

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #6

P: n/a
Michael (michka) Kaplan [MS] <mi*****@online.microsoft.com> wrote:
UTF-8 works with bytes....


Well, I wouldn't say it's an "8-bit encoding" in the normal sense of
the phrase...

Not every character in the set (in fact, very few!) can be represented
as a single byte.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote...
Not every character in the set (in fact, very few!) can be represented
as a single byte.


Yes -- but Unicode covers a lot of ground. Attempts to do less lead to
corruption of text in the "lesser" code page, and I am reasonably certasin
that such corruption is never a good thing....

:-)

--
MichKa [MS]
NLS Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies
Windows International Division

This posting is provided "AS IS" with
no warranties, and confers no rights.
Nov 16 '05 #8

P: n/a
Michael (michka) Kaplan [MS] <mi*****@online.microsoft.com> wrote:
Not every character in the set (in fact, very few!) can be represented
as a single byte.


Yes -- but Unicode covers a lot of ground. Attempts to do less lead to
corruption of text in the "lesser" code page, and I am reasonably certasin
that such corruption is never a good thing....


That's pretty much irrelevant when the encoding is fixed to start with
though, as the OP says it is. The best you can do is detect that you're
trying to write a character which isn't in the target character set,
and either throw an exception or write something else (eg '?').

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

P: n/a
MA

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
MA <ne**@supremelink.se> wrote:
I have a major problem. I need to write an textfile with 1 b per letter.
But
it should be able to handle swedish letters to ().
Is it possible to use 8 b ASCII for this?
There's no such thing as "8 bit ASCII" (assuming that's what you meant
by "b").
This file is used by an sms application and cannot be in another
format.


You need to find out *exactly* what encoding will be used. There are
various 8 bit character sets which are compatible with ASCII in the
range 0-127, but which are incompatible with each other above 127. If
you can find out which of those your app needs to output, it should be
easy to find the appropriate Encoding to give to your StreamWriter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Ok. Is it ASCII-8 then?

Well, I solved it by using this code:

char[] test = mailContent.ToCharArray();

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("437");

System.Text.Encoder ence = enc.GetEncoder();

FileStream fsWriter = new FileStream(filePath + fileName,
System.IO.FileMode.Create);

byte[] bytes = new Byte[ence.GetByteCount(test,0, test.Length, true)];

ence.GetBytes(test, 0, test.Length, bytes, 0, true);

fsWriter.Write(bytes, 0, bytes.Length);

/Marre
Nov 16 '05 #10

P: n/a
[Please fix your newsreader to quote properly, btw - it's a pain to
reply when you follow-up in the way you have, especially including the
sig separator from my post]

MA <ne**@supremelink.se> wrote:
Ok. Is it ASCII-8 then?
Is what "ASCII-8", exactly? Can't say I've heard of that before.
Well, I solved it by using this code:

char[] test = mailContent.ToCharArray();

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("437");

System.Text.Encoder ence = enc.GetEncoder();

FileStream fsWriter = new FileStream(filePath + fileName,
System.IO.FileMode.Create);

byte[] bytes = new Byte[ence.GetByteCount(test,0, test.Length, true)];

ence.GetBytes(test, 0, test.Length, bytes, 0, true);

fsWriter.Write(bytes, 0, bytes.Length);


Just using StreamReader with an encoding of Encoding.GetEncoding("437")
would be rather simpler. However, are you *really* sure it's code page
437? If you've got the wrong code page, you'll write out the wrong
characters sooner or later.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

P: n/a
MA
> Is what "ASCII-8", exactly? Can't say I've heard of that before.
Well, as you might have noticed, Im really a newbie here :) I looked at
this link:
http://homepage.cs.uri.edu/faculty/w...pleteASCII.htm

Just using StreamReader with an encoding of Encoding.GetEncoding("437")
would be rather simpler. However, are you *really* sure it's code page
437? If you've got the wrong code page, you'll write out the wrong
characters sooner or later.

Yes. According to those who have developed the app Im 'talking' with, its
437.
Its not that important if some character get wrong, but if it happends, I
will probably return to this newsgroup :)

Sorry for my quote-problem. Never thougt of that before.

/Marre
Nov 16 '05 #12

P: n/a
MA <ne**@supremelink.se> wrote:
Is what "ASCII-8", exactly? Can't say I've heard of that before. Well, as you might have noticed, Im really a newbie here :) I looked at
this link:
http://homepage.cs.uri.edu/faculty/w...pleteASCII.htm


Any page which claims ASCII has characters over 127 shouldn't be
trusted, I'm afraid...
Just using StreamReader with an encoding of Encoding.GetEncoding("437")
would be rather simpler. However, are you *really* sure it's code page
437? If you've got the wrong code page, you'll write out the wrong
characters sooner or later.

Yes. According to those who have developed the app Im 'talking' with, its
437.


Good. It's always nice when you don't have to guess :)
Its not that important if some character get wrong, but if it happends, I
will probably return to this newsgroup :)
Righto.
Sorry for my quote-problem. Never thougt of that before.


No problem - thanks for fixing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.