Returning ??? using System.Text.ASCIIEncoding

D

Dan

When I use the following code I get "???" in place of the non-Ascii
character. Is there any way to fix the conversion or do I have to write a
function to search for "???" and replace with another character?
Any help or suggestions would be greatly appreciated.
Thanks
Dan

Dim plainText As String
plainText = "tâ””he"
Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(plainTextBytes).ToString
 
A

Armin Zingler

Dan said:
When I use the following code I get "???" in place of the non-Ascii
character. Is there any way to fix the conversion or do I have to
write a function to search for "???" and replace with another
character? Any help or suggestions would be greatly appreciated.
Thanks
Dan

Dim plainText As String
plainText = "t+he"
Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(plainTextBytes).ToString


You're mixing different Encodings. In 'plainTextBytes' you have UTF8 encoded
characters. Then you assume they are ASCII encoded, but they are not. ASCII
= 7bits only, so values >127 will result in a questionmark.
What are you trying to do?


Armin
 
H

Herfried K. Wagner [MVP]

Dan said:
When I use the following code I get "???" in place of the non-Ascii
character. Is there any way to fix the conversion or do I have to write a
function to search for "???" and replace with another character?

Dim plainText As String
plainText = "tâ””he"
Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(plainTextBytes).ToString

What exactly do you want to achieve? Converting text using Unicode
characters not being present in ASCII will cause information loss. Those
characters are represented by the question mark.
 
M

Michel Posseth

Dan schreef:
When I use the following code I get "???" in place of the non-Ascii
character. Is there any way to fix the conversion or do I have to write a
function to search for "???" and replace with another character?
Any help or suggestions would be greatly appreciated.
Thanks
Dan

Dim plainText As String
plainText = "tâ””he"
Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(plainTextBytes).ToString
if the characters are not present in the local ascii character tabel
well then you have a problem when you convert unicode to ascii

This is inreversible once it is ??? it stays corrupted , you would best
use unicode as this can hold all characters including chinese .

if you need it to be unicode you must make sure that you are running on
a computer with the correct character tables installed


HTH

Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top