Returning ??? using System.Text.ASCIIEncoding

  • Thread starter Thread starter Dan
  • Start date Start date
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
 
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
 
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.
 
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
 
Back
Top