473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Unicode to ASCII string conversion

Ger
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?

I found Encoding.Convert, but that needs byte arrays.

Thanks,
/Ger
Nov 21 '05 #1
18 34030
* "Ger" <ge*********@remove-this-part-of-address.planet.nl> scripsit:
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?


'System.Text.Encoding.ASCII.GetBytes',
'System.Text.Encoding.Unicode.GetByte'.

BTW: Notice that there is no 1:1 mapping defined between Unicode and
ASCII, ASCII is 7-bit only and can thus only represent 128 characters.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Ger,
In addition to Herfried's comments.

For information on Unicode, ASCII and Encoding in .NET see:

http://www.yoda.arachsys.com/csharp/unicode.html

The samples may be in C#, however they should easily be converted to VB.NET

Hope this helps
Jay

"Ger" <ge*********@remove-this-part-of-address.planet.nl> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?

I found Encoding.Convert, but that needs byte arrays.

Thanks,
/Ger

Nov 21 '05 #3
Ger
Thanks Jay, a most useful document.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:ee**************@TK2MSFTNGP11.phx.gbl...
Ger,
In addition to Herfried's comments.

For information on Unicode, ASCII and Encoding in .NET see:

http://www.yoda.arachsys.com/csharp/unicode.html

The samples may be in C#, however they should easily be converted to VB.NET
Hope this helps
Jay

"Ger" <ge*********@remove-this-part-of-address.planet.nl> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?

I found Encoding.Convert, but that needs byte arrays.

Thanks,
/Ger


Nov 21 '05 #4
Ger

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
* "Ger" <ge*********@remove-this-part-of-address.planet.nl> scripsit:
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?


'System.Text.Encoding.ASCII.GetBytes',
'System.Text.Encoding.Unicode.GetByte'.

Thanks for your reply, but this returns a byte array. I ment straight
forward string-to-string conversion. It is possible ofcourse to write a
simple function to do this and using the encoding class, but I was just
wondering why the framework does not support the "direct string-to-string".
/Ger
Nov 21 '05 #5
Ger,
Thanks for your reply, but this returns a byte array. I ment straight
forward string-to-string conversion. It is possible ofcourse to write a
simple function to do this and using the encoding class, but I was just
wondering why the framework does not support the "direct

string-to-string".

In the dotNet is a "String" is forever a string of unicode Chars. What you
call "ascii string" is forever a bytearray.

Therefore as an answer there is nothing more than Herfried suggested.
Although you can create an array of objects which contains bytes, however
that is no solution in my opinion.

I hope this helps to get the idea?

Cor

Nov 21 '05 #6
Ger
Ah, now I think I get the idea. So when I convert a (unicode) string into an
ascii byte array, and then the byte array back into a string, I still have
Unicode, right? So that is of no use when you want to write ASCII to a
filestream.

Is the code below then writing ASCII output to my filestream?

Dim UnicodeString As String = "abcdëfg"
Dim fsOutput as New FileStream(..)
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
wOutput.WriteLine(UnicodeString)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@planet.nl> schreef in bericht
news:eW****************@tk2msftngp13.phx.gbl...
Ger,
Thanks for your reply, but this returns a byte array. I ment straight
forward string-to-string conversion. It is possible ofcourse to write a
simple function to do this and using the encoding class, but I was just
wondering why the framework does not support the "direct

string-to-string".

In the dotNet is a "String" is forever a string of unicode Chars. What you
call "ascii string" is forever a bytearray.

Therefore as an answer there is nothing more than Herfried suggested.
Although you can create an array of objects which contains bytes, however
that is no solution in my opinion.

I hope this helps to get the idea?

Cor

Nov 21 '05 #7
Ger
Ah, now I think I get the idea. So when I convert a (unicode) string into an ascii byte array, and then the byte array back into a string, I still have
Unicode, right? So that is of no use when you want to write ASCII to a
filestream.


That I do not say, however when you read it back in a String you have again
a string of Chars.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor
Nov 21 '05 #8
Ger,
Ah, now I think I get the idea. So when I convert a (Unicode) string into
an
ascii byte array, and then the byte array back into a string, I still have
Unicode, right? Correct, just remember that you will loose some characters going to & from
ASCII.
So that is of no use when you want to write ASCII to a
filestream. If you need an ASCII file, then use a ASCII encoding. It really depends on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding. With
ASCII you will loose all extended characters (ASCII is 7 bit encoding), with
ANSI you will loose characters that are outside of your regional ANSI code
page. UTF8 preserves all Unicode characters. I would recommend ANSI encoding
if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both 8
bit encodings.

Is the code below then writing ASCII output to my filestream?
Yes that code is writing ASCII, as you included the ASCII encoding on the
StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently
open that text stream and read it (with a StreamReader) it will be converted
back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream
writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)
Which will write the file in your current ANSI code page as defined by the
regional settings in Windows Control Panel. Which will preserve extended
characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code
page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl... Ah, now I think I get the idea. So when I convert a (unicode) string into
an
ascii byte array, and then the byte array back into a string, I still have
Unicode, right? So that is of no use when you want to write ASCII to a
filestream.

Is the code below then writing ASCII output to my filestream?

Dim UnicodeString As String = "abcdëfg"
Dim fsOutput as New FileStream(..)
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
wOutput.WriteLine(UnicodeString)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@planet.nl> schreef in bericht
news:eW****************@tk2msftngp13.phx.gbl...
Ger,
> Thanks for your reply, but this returns a byte array. I ment straight
> forward string-to-string conversion. It is possible ofcourse to write a
> simple function to do this and using the encoding class, but I was just
> wondering why the framework does not support the "direct

string-to-string".

In the dotNet is a "String" is forever a string of unicode Chars. What
you
call "ascii string" is forever a bytearray.

Therefore as an answer there is nothing more than Herfried suggested.
Although you can create an array of objects which contains bytes, however
that is no solution in my opinion.

I hope this helps to get the idea?

Cor


Nov 21 '05 #9
Ger
Thank you very much guys for your help and clearing up the issue for me.
I will go for Jay's solution and use ANSI 8-bit.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ger,
Ah, now I think I get the idea. So when I convert a (Unicode) string into an
ascii byte array, and then the byte array back into a string, I still have Unicode, right? Correct, just remember that you will loose some characters going to & from
ASCII.
So that is of no use when you want to write ASCII to a
filestream.

If you need an ASCII file, then use a ASCII encoding. It really depends on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding. With
ASCII you will loose all extended characters (ASCII is 7 bit encoding),

with ANSI you will loose characters that are outside of your regional ANSI code
page. UTF8 preserves all Unicode characters. I would recommend ANSI encoding if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both 8
bit encodings.

Is the code below then writing ASCII output to my filestream?
Yes that code is writing ASCII, as you included the ASCII encoding on the
StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently
open that text stream and read it (with a StreamReader) it will be

converted back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream
writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)


Which will write the file in your current ANSI code page as defined by the
regional settings in Windows Control Panel. Which will preserve extended
characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code
page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
Ah, now I think I get the idea. So when I convert a (unicode) string into an
ascii byte array, and then the byte array back into a string, I still have Unicode, right? So that is of no use when you want to write ASCII to a
filestream.

Is the code below then writing ASCII output to my filestream?

Dim UnicodeString As String = "abcdëfg"
Dim fsOutput as New FileStream(..)
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
wOutput.WriteLine(UnicodeString)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@planet.nl> schreef in bericht
news:eW****************@tk2msftngp13.phx.gbl...
Ger,

> Thanks for your reply, but this returns a byte array. I ment straight
> forward string-to-string conversion. It is possible ofcourse to write a > simple function to do this and using the encoding class, but I was just > wondering why the framework does not support the "direct
string-to-string".

In the dotNet is a "String" is forever a string of unicode Chars. What
you
call "ascii string" is forever a bytearray.

Therefore as an answer there is nothing more than Herfried suggested.
Although you can create an array of objects which contains bytes, however that is no solution in my opinion.

I hope this helps to get the idea?

Cor



Nov 21 '05 #10
Jay,

Because of Ger's answer, now I become curious. I did not read it in your
message, however what is the solution, Ger told he wanted a straight string
to string conversion and explicitly no bytearray, however now I understand
he can convert Unicode to a 8 bits ANSI String in VBNet? (And I am not
talking about writing a file with 8 bits chars by decoding the char)

I showed in this thread with a link to an MSDN page that a String contains
forever 16 bits Chars.

Is that documentation wrong or do I not understand it or maybe even
something complete different..

Cor


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ger,
Ah, now I think I get the idea. So when I convert a (Unicode) string into an
ascii byte array, and then the byte array back into a string, I still have Unicode, right? Correct, just remember that you will loose some characters going to & from
ASCII.
So that is of no use when you want to write ASCII to a
filestream.

If you need an ASCII file, then use a ASCII encoding. It really depends on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding. With
ASCII you will loose all extended characters (ASCII is 7 bit encoding),

with ANSI you will loose characters that are outside of your regional ANSI code
page. UTF8 preserves all Unicode characters. I would recommend ANSI encoding if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both 8
bit encodings.

Is the code below then writing ASCII output to my filestream?
Yes that code is writing ASCII, as you included the ASCII encoding on the
StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently
open that text stream and read it (with a StreamReader) it will be

converted back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream
writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)


Which will write the file in your current ANSI code page as defined by the
regional settings in Windows Control Panel. Which will preserve extended
characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code
page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
Ah, now I think I get the idea. So when I convert a (unicode) string into an
ascii byte array, and then the byte array back into a string, I still have Unicode, right? So that is of no use when you want to write ASCII to a
filestream.

Is the code below then writing ASCII output to my filestream?

Dim UnicodeString As String = "abcdëfg"
Dim fsOutput as New FileStream(..)
Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
wOutput.WriteLine(UnicodeString)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@planet.nl> schreef in bericht
news:eW****************@tk2msftngp13.phx.gbl...
Ger,

> Thanks for your reply, but this returns a byte array. I ment straight
> forward string-to-string conversion. It is possible ofcourse to write a > simple function to do this and using the encoding class, but I was just > wondering why the framework does not support the "direct
string-to-string".

In the dotNet is a "String" is forever a string of unicode Chars. What
you
call "ascii string" is forever a bytearray.

Therefore as an answer there is nothing more than Herfried suggested.
Although you can create an array of objects which contains bytes, however that is no solution in my opinion.

I hope this helps to get the idea?

Cor



Nov 21 '05 #11
Ger,
I should add that I would normally use Encoding.Default (ANSI) for straight
text files. I would normally use UTF8 for XML files.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:Oy*************@TK2MSFTNGP14.phx.gbl...
Thank you very much guys for your help and clearing up the issue for me.
I will go for Jay's solution and use ANSI 8-bit.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ger,
> Ah, now I think I get the idea. So when I convert a (Unicode) string into > an
> ascii byte array, and then the byte array back into a string, I still have > Unicode, right?

Correct, just remember that you will loose some characters going to &
from
ASCII.
> So that is of no use when you want to write ASCII to a
> filestream.

If you need an ASCII file, then use a ASCII encoding. It really depends
on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding.
With
ASCII you will loose all extended characters (ASCII is 7 bit encoding),

with
ANSI you will loose characters that are outside of your regional ANSI
code
page. UTF8 preserves all Unicode characters. I would recommend ANSI

encoding
if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both
8
bit encodings.

> Is the code below then writing ASCII output to my filestream?


Yes that code is writing ASCII, as you included the ASCII encoding on the
StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently
open that text stream and read it (with a StreamReader) it will be

converted
back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to
read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode
characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream
writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:
> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)


Which will write the file in your current ANSI code page as defined by
the
regional settings in Windows Control Panel. Which will preserve extended
characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code
page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
> Ah, now I think I get the idea. So when I convert a (unicode) string into > an
> ascii byte array, and then the byte array back into a string, I still have > Unicode, right? So that is of no use when you want to write ASCII to a
> filestream.
>
> Is the code below then writing ASCII output to my filestream?
>
> Dim UnicodeString As String = "abcdëfg"
> Dim fsOutput as New FileStream(..)
> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
> wOutput.WriteLine(UnicodeString)
>
> Thank you for your reply.
>
> /Ger
>
>
> "Cor Ligthert" <no**********@planet.nl> schreef in bericht
> news:eW****************@tk2msftngp13.phx.gbl...
>> Ger,
>>
>> > Thanks for your reply, but this returns a byte array. I ment
>> > straight
>> > forward string-to-string conversion. It is possible ofcourse to
>> > write a >> > simple function to do this and using the encoding class, but I was just >> > wondering why the framework does not support the "direct
>> string-to-string".
>>
>> In the dotNet is a "String" is forever a string of unicode Chars. What
>> you
>> call "ascii string" is forever a bytearray.
>>
>> Therefore as an answer there is nothing more than Herfried suggested.
>> Although you can create an array of objects which contains bytes, however >> that is no solution in my opinion.
>>
>> I hope this helps to get the idea?
>>
>> Cor
>>
>>
>>
>
>



Nov 21 '05 #12
Cor,
Read my post. ;-) I only discussed reading & writing strings to ASCII, ANSI,
and UTF8 files (7 & 8 bit encodings).

You are correct System.String & System.Char are UTF-16 (16 bit Unicode),
files can be ANSI, ASCII, UTF7, UTF8, EBCDIC, UTF16 and many other
encodings.
FWIW: VS.NET 2005 (.NET 2.0, aka Whidbey, due out in 2005) appears to
support UTF-32 encoding for files.

http://msdn2.microsoft.com/library/ts575t62.aspx

Hope this helps
Jay
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Jay,

Because of Ger's answer, now I become curious. I did not read it in your
message, however what is the solution, Ger told he wanted a straight
string
to string conversion and explicitly no bytearray, however now I understand
he can convert Unicode to a 8 bits ANSI String in VBNet? (And I am not
talking about writing a file with 8 bits chars by decoding the char)

I showed in this thread with a link to an MSDN page that a String contains
forever 16 bits Chars.

Is that documentation wrong or do I not understand it or maybe even
something complete different..

Cor


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ger,
> Ah, now I think I get the idea. So when I convert a (Unicode) string into > an
> ascii byte array, and then the byte array back into a string, I still have > Unicode, right?

Correct, just remember that you will loose some characters going to &
from
ASCII.
> So that is of no use when you want to write ASCII to a
> filestream.

If you need an ASCII file, then use a ASCII encoding. It really depends
on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding.
With
ASCII you will loose all extended characters (ASCII is 7 bit encoding),

with
ANSI you will loose characters that are outside of your regional ANSI
code
page. UTF8 preserves all Unicode characters. I would recommend ANSI

encoding
if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both
8
bit encodings.

> Is the code below then writing ASCII output to my filestream?


Yes that code is writing ASCII, as you included the ASCII encoding on the
StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently
open that text stream and read it (with a StreamReader) it will be

converted
back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to
read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode
characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream
writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:
> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)


Which will write the file in your current ANSI code page as defined by
the
regional settings in Windows Control Panel. Which will preserve extended
characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code
page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
> Ah, now I think I get the idea. So when I convert a (unicode) string into > an
> ascii byte array, and then the byte array back into a string, I still have > Unicode, right? So that is of no use when you want to write ASCII to a
> filestream.
>
> Is the code below then writing ASCII output to my filestream?
>
> Dim UnicodeString As String = "abcdëfg"
> Dim fsOutput as New FileStream(..)
> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
> wOutput.WriteLine(UnicodeString)
>
> Thank you for your reply.
>
> /Ger
>
>
> "Cor Ligthert" <no**********@planet.nl> schreef in bericht
> news:eW****************@tk2msftngp13.phx.gbl...
>> Ger,
>>
>> > Thanks for your reply, but this returns a byte array. I ment
>> > straight
>> > forward string-to-string conversion. It is possible ofcourse to
>> > write a >> > simple function to do this and using the encoding class, but I was just >> > wondering why the framework does not support the "direct
>> string-to-string".
>>
>> In the dotNet is a "String" is forever a string of unicode Chars. What
>> you
>> call "ascii string" is forever a bytearray.
>>
>> Therefore as an answer there is nothing more than Herfried suggested.
>> Although you can create an array of objects which contains bytes, however >> that is no solution in my opinion.
>>
>> I hope this helps to get the idea?
>>
>> Cor
>>
>>
>>
>
>



Nov 21 '05 #13
Ger
Jay,
Actually the reason I bothered this group with my question was because my
app generates and writes HTML data to a file believing it would be
interpreted by the browser as ASCII by setting charset=windows-1252 in the
meta tag of the htlm head.
Then I found that certain diacritical characters were not well interpreted
by the browser, and I found that that was because of the characters in the
generated stream were delivered as Unicode. After changing the meta tag in
the HTML code to charset=UTF-8 all was fine, but it left me with the
question on how these things work, hence why there was no simple
string-to-string conversion in the framework.
Your and Cor's replies made me understand better how these things work in
..Net, and I thank you both for your help in this.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:uO**************@TK2MSFTNGP11.phx.gbl...
Ger,
I should add that I would normally use Encoding.Default (ANSI) for straight text files. I would normally use UTF8 for XML files.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:Oy*************@TK2MSFTNGP14.phx.gbl...
Thank you very much guys for your help and clearing up the issue for me.
I will go for Jay's solution and use ANSI 8-bit.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ger,
> Ah, now I think I get the idea. So when I convert a (Unicode) string

into
> an
> ascii byte array, and then the byte array back into a string, I still

have
> Unicode, right?
Correct, just remember that you will loose some characters going to &
from
ASCII.

> So that is of no use when you want to write ASCII to a
> filestream.
If you need an ASCII file, then use a ASCII encoding. It really depends
on
what is going to read the file again.

I would recommend with an ANSI encoding (see below) or UTF8 encoding.
With
ASCII you will loose all extended characters (ASCII is 7 bit encoding),

with
ANSI you will loose characters that are outside of your regional ANSI
code
page. UTF8 preserves all Unicode characters. I would recommend ANSI

encoding
if the file was going to be opened by casual users in Notepad. I would
recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both 8
bit encodings.
> Is the code below then writing ASCII output to my filestream?

Yes that code is writing ASCII, as you included the ASCII encoding on the StreamWriter constructor.

The text file itself will contain ASCII characters, when you subsequently open that text stream and read it (with a StreamReader) it will be

converted
back to Unicode strings. When reading the file back try to use the same
encoding as written. For example if you wrote ANSI, then use ANSI to
read.
If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode
characters
127 to 255 differently. Remember that Encoding.UTF8 is used on the stream writer if you do not give one, if you are reading text files created by
Notepad, then you want Encoding.Default.

I would recommend:

> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default)
Which will write the file in your current ANSI code page as defined by
the
regional settings in Windows Control Panel. Which will preserve extended characters.

Remember that ANSI is an 8 bit encoding that is dependent on region (code page). While ASCII is a 7 bit encoding, ASCII does not support extended
characters such as ë. It will be converted into either a normal e or a ?.
Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
> Ah, now I think I get the idea. So when I convert a (unicode) string

into
> an
> ascii byte array, and then the byte array back into a string, I still

have
> Unicode, right? So that is of no use when you want to write ASCII to a > filestream.
>
> Is the code below then writing ASCII output to my filestream?
>
> Dim UnicodeString As String = "abcdëfg"
> Dim fsOutput as New FileStream(..)
> Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.ASCII)
> wOutput.WriteLine(UnicodeString)
>
> Thank you for your reply.
>
> /Ger
>
>
> "Cor Ligthert" <no**********@planet.nl> schreef in bericht
> news:eW****************@tk2msftngp13.phx.gbl...
>> Ger,
>>
>> > Thanks for your reply, but this returns a byte array. I ment
>> > straight
>> > forward string-to-string conversion. It is possible ofcourse to
>> > write

a
>> > simple function to do this and using the encoding class, but I was

just
>> > wondering why the framework does not support the "direct
>> string-to-string".
>>
>> In the dotNet is a "String" is forever a string of unicode Chars. What >> you
>> call "ascii string" is forever a bytearray.
>>
>> Therefore as an answer there is nothing more than Herfried suggested. >> Although you can create an array of objects which contains bytes,

however
>> that is no solution in my opinion.
>>
>> I hope this helps to get the idea?
>>
>> Cor
>>
>>
>>
>
>



Nov 21 '05 #14
Ger,
interpreted by the browser as ASCII by setting charset=windows-1252 in the charset=windows-1252 is ANSI, not ASCII!

If you use Encoding.Default you will (normally) wind up with windows-1252 in
the US and some of Europe.

Try the following:

Debug.WriteLine(System.Text.Encoding.Default.Encod ingName, "encoding
name")
Debug.WriteLine(System.Text.Encoding.Default.BodyN ame, "body name")
Debug.WriteLine(System.Text.Encoding.Default.Heade rName, "header
name")
Debug.WriteLine(System.Text.Encoding.Default.WebNa me, "web name")
Debug.WriteLine(System.Text.Encoding.Default.CodeP age, "code page")
Debug.WriteLine(System.Text.Encoding.Default.Windo wsCodePage,
"windows code page")

I would expect Encoding.Default with charset = Encoding.Default.WebName
should give you the effect you desire, with any regional settings.

Of course UTF8 will preserve ALL unicode characters in your output (good for
XML & HTML).

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:un**************@TK2MSFTNGP11.phx.gbl... Jay,
Actually the reason I bothered this group with my question was because my
app generates and writes HTML data to a file believing it would be
interpreted by the browser as ASCII by setting charset=windows-1252 in the
meta tag of the htlm head.
Then I found that certain diacritical characters were not well interpreted
by the browser, and I found that that was because of the characters in the
generated stream were delivered as Unicode. After changing the meta tag in
the HTML code to charset=UTF-8 all was fine, but it left me with the
question on how these things work, hence why there was no simple
string-to-string conversion in the framework.
Your and Cor's replies made me understand better how these things work in
.Net, and I thank you both for your help in this.
/Ger

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
news:uO**************@TK2MSFTNGP11.phx.gbl...
Ger,
I should add that I would normally use Encoding.Default (ANSI) for

straight
text files. I would normally use UTF8 for XML files.

Hope this helps
Jay

"Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
news:Oy*************@TK2MSFTNGP14.phx.gbl...
> Thank you very much guys for your help and clearing up the issue for
> me.
> I will go for Jay's solution and use ANSI 8-bit.
> /Ger
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
> bericht
> news:%2****************@TK2MSFTNGP10.phx.gbl...
>> Ger,
>> > Ah, now I think I get the idea. So when I convert a (Unicode) string
> into
>> > an
>> > ascii byte array, and then the byte array back into a string, I
>> > still
> have
>> > Unicode, right?
>> Correct, just remember that you will loose some characters going to &
>> from
>> ASCII.
>>
>> > So that is of no use when you want to write ASCII to a
>> > filestream.
>> If you need an ASCII file, then use a ASCII encoding. It really
>> depends
>> on
>> what is going to read the file again.
>>
>> I would recommend with an ANSI encoding (see below) or UTF8 encoding.
>> With
>> ASCII you will loose all extended characters (ASCII is 7 bit
>> encoding),
> with
>> ANSI you will loose characters that are outside of your regional ANSI
>> code
>> page. UTF8 preserves all Unicode characters. I would recommend ANSI
> encoding
>> if the file was going to be opened by casual users in Notepad. I would
>> recommend UTF8 if full Unicode support is required. ANSI & UTF8 are both >> 8
>> bit encodings.
>>
>>
>> > Is the code below then writing ASCII output to my filestream?
>>
>> Yes that code is writing ASCII, as you included the ASCII encoding on the >> StreamWriter constructor.
>>
>> The text file itself will contain ASCII characters, when you subsequently >> open that text stream and read it (with a StreamReader) it will be
> converted
>> back to Unicode strings. When reading the file back try to use the
>> same
>> encoding as written. For example if you wrote ANSI, then use ANSI to
>> read.
>> If you wrote UTF8, then use UTF8 to read. As ANSI & UTF8 encode
>> characters
>> 127 to 255 differently. Remember that Encoding.UTF8 is used on the stream >> writer if you do not give one, if you are reading text files created
>> by
>> Notepad, then you want Encoding.Default.
>>
>> I would recommend:
>>
>> > Dim wOutput As New StreamWriter(fsOutput, System.Text.Encoding.Default) >>
>> Which will write the file in your current ANSI code page as defined by
>> the
>> regional settings in Windows Control Panel. Which will preserve extended >> characters.
>>
>> Remember that ANSI is an 8 bit encoding that is dependent on region (code >> page). While ASCII is a 7 bit encoding, ASCII does not support
>> extended
>> characters such as ë. It will be converted into either a normal e or a ?. >>
>> Hope this helps
>> Jay
>>
>> "Ger" <ge*********@rathernospam.sailsoft.nl> wrote in message
>> news:uU**************@tk2msftngp13.phx.gbl...
>> > Ah, now I think I get the idea. So when I convert a (unicode) string
> into
>> > an
>> > ascii byte array, and then the byte array back into a string, I
>> > still
> have
>> > Unicode, right? So that is of no use when you want to write ASCII to a >> > filestream.
>> >
>> > Is the code below then writing ASCII output to my filestream?
>> >
>> > Dim UnicodeString As String = "abcdëfg"
>> > Dim fsOutput as New FileStream(..)
>> > Dim wOutput As New StreamWriter(fsOutput,
>> > System.Text.Encoding.ASCII)
>> > wOutput.WriteLine(UnicodeString)
>> >
>> > Thank you for your reply.
>> >
>> > /Ger
>> >
>> >
>> > "Cor Ligthert" <no**********@planet.nl> schreef in bericht
>> > news:eW****************@tk2msftngp13.phx.gbl...
>> >> Ger,
>> >>
>> >> > Thanks for your reply, but this returns a byte array. I ment
>> >> > straight
>> >> > forward string-to-string conversion. It is possible ofcourse to
>> >> > write
> a
>> >> > simple function to do this and using the encoding class, but I
>> >> > was
> just
>> >> > wondering why the framework does not support the "direct
>> >> string-to-string".
>> >>
>> >> In the dotNet is a "String" is forever a string of unicode Chars. What >> >> you
>> >> call "ascii string" is forever a bytearray.
>> >>
>> >> Therefore as an answer there is nothing more than Herfried suggested. >> >> Although you can create an array of objects which contains bytes,
> however
>> >> that is no solution in my opinion.
>> >>
>> >> I hope this helps to get the idea?
>> >>
>> >> Cor
>> >>
>> >>
>> >>
>> >
>> >
>>
>>
>
>



Nov 21 '05 #15
Jay,
If you use Encoding.Default you will (normally) wind up with windows-1252 in the US and some of Europe.

I think that I have confused you, therefore I have checked it, as far as I
can see it is only not used in the Cyriclic writing European countries.

See the link bellow

And I miss even some languages on that as from Kroatia, Slovenia (Miha),
Slowaaks and I am in doubt about Turkish

So probably European countries which not uses 1252 are where is spoken
Greek
Rumanian (retroromanish is one of the 4 in Swiss spoken languages)
Serbians,
Hertza/Bosnie,
Russian

Look at the bottom of the page, the page is in German however I think easy
to understand

http://de.wikipedia.org/wiki/Windows-1252#Windows-1252

So next time you can in my opinion write "most European"

I hope this gives again some ideas

Cor
Nov 21 '05 #16
Cor,
Aren't some of the European countries one of two encodings, such as the
Netherlands? Or is that just the DOS code page? (Trying to remember
something I thought you've stated before).

Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
Jay,
If you use Encoding.Default you will (normally) wind up with windows-1252

in
the US and some of Europe.

I think that I have confused you, therefore I have checked it, as far as I
can see it is only not used in the Cyriclic writing European countries.

See the link bellow

And I miss even some languages on that as from Kroatia, Slovenia (Miha),
Slowaaks and I am in doubt about Turkish

So probably European countries which not uses 1252 are where is spoken
Greek
Rumanian (retroromanish is one of the 4 in Swiss spoken languages)
Serbians,
Hertza/Bosnie,
Russian

Look at the bottom of the page, the page is in German however I think easy
to understand

http://de.wikipedia.org/wiki/Windows-1252#Windows-1252

So next time you can in my opinion write "most European"

I hope this gives again some ideas

Cor

Nov 21 '05 #17
Jay,

Therefore I checked it, I have confused you where I was talking about the
DOS 437 and 850 code page and you never knew what was used, however we use
now probably all the 1252.

Cor
Aren't some of the European countries one of two encodings, such as the
Netherlands? Or is that just the DOS code page? (Trying to remember
something I thought you've stated before).

Nov 21 '05 #18
br................
Therefore I checked it, I have confused you where I was talking about the
DOS 437 and 850 code page and you never knew what was used, however we use now probably all the 1252.


That "you" never knew is not Jay, just as saying.
Nov 21 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Willem | last post by:
What is the best way to calculate an ascii string into an integer (not talking about an atoi conversion): For examle if I have the ascii string: "/b" then in hex it would be 2F7A and if I...
1
by: N | last post by:
Hi, I'm writing a small web service (using C#) which is going to receive a text file, add a line to it and send it back. Input is a string with each line ending with "\r\n". The problem is in the...
19
by: Thomas W | last post by:
I'm getting really annoyed with python in regards to unicode/ascii-encoding problems. The string below is the encoding of the norwegian word "fødselsdag". I stored the string as "fødselsdag"...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
9
by: thijs.braem | last post by:
Hi everyone, I'm having quite some troubles trying to convert Unicode to String (for use in psycopg, which apparently doesn't know how to cope with unicode strings). The error I keep having...
4
by: vcnewbie | last post by:
Hi I'm maintaining a VisualC++ project to increase its security regarding stored passwords. I thought about using SHA256Managed to create a hash for the password when creating a user and when...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.