473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Conver t, but that needs byte arrays.

Thanks,
/Ger
Nov 21 '05 #1
18 34142
* "Ger" <ge*********@re move-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.En coding.ASCII.Ge tBytes',
'System.Text.En coding.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*********@re move-this-part-of-address.planet. nl> wrote in message
news:up******** ******@TK2MSFTN GP09.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.Conver t, 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******** ******@TK2MSFTN GP11.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*********@re move-this-part-of-address.planet. nl> wrote in message
news:up******** ******@TK2MSFTN GP09.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.Conver t, but that needs byte arrays.

Thanks,
/Ger


Nov 21 '05 #4
Ger

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schreef in bericht
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
* "Ger" <ge*********@re move-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.En coding.ASCII.Ge tBytes',
'System.Text.En coding.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(fs Output, System.Text.Enc oding.ASCII)
wOutput.WriteLi ne(UnicodeStrin g)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@p lanet.nl> schreef in bericht
news:eW******** ********@tk2msf tngp13.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.Defaul t.

I would recommend:
Dim wOutput As New StreamWriter(fs Output, System.Text.Enc oding.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*********@ra thernospam.sail soft.nl> wrote in message
news:uU******** ******@tk2msftn gp13.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(fs Output, System.Text.Enc oding.ASCII)
wOutput.WriteLi ne(UnicodeStrin g)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@p lanet.nl> schreef in bericht
news:eW******** ********@tk2msf tngp13.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******** ********@TK2MSF TNGP10.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.Defaul t.

I would recommend:
Dim wOutput As New StreamWriter(fs Output, System.Text.Enc oding.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*********@ra thernospam.sail soft.nl> wrote in message
news:uU******** ******@tk2msftn gp13.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(fs Output, System.Text.Enc oding.ASCII)
wOutput.WriteLi ne(UnicodeStrin g)

Thank you for your reply.

/Ger
"Cor Ligthert" <no**********@p lanet.nl> schreef in bericht
news:eW******** ********@tk2msf tngp13.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

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

Similar topics

6
2035
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 convert that to decimal I would get 12154. I can't figure out how to concatenate? my hex values together if that makes any sense? Any pointers would be greatly appreciated!
1
1839
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 format of the output - looks like all ends of lines are missing, even if web service just receives and sends back the same string. Could it be some encoding issue?
19
3332
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" but somewhere in my code it got translated into the mess above and I cannot get the original string back. It cannot be printed in the console or written a plain text-file. I've tried to convert it using
2
3321
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 "D:\projects\sitetruth\InfoSitePage.py", line 285, in httpfetch sitetext = sitetext.encode('ascii','replace') # force to clean ASCII UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 29151: ordinal not in range(128)
9
15737
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 is something like this: ERREUR: Séquence d'octets invalide pour le codage «UTF8» : 0xe02063 (sorry, locale is french, it means "byte sequence invalid for encoding
4
2242
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 this new user tries to login, a new hash will be created for the given password and compared to the stored hash. I guess this is quite common.
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.