Converting UTF8 -> ASCII

C

C# Learner

I'm trying to convert a UTF8 string to an ASCII string, but the code
I've tried isn't working.

<code>

private static string Utf8ToAscii(string value)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(value);
byte[] asciiBytes = Encoding.Convert(Encoding.UTF8,
Encoding.ASCII, utf8Bytes);

return Encoding.ASCII.GetString(asciiBytes);
}

</code>

Am I doing something wrong?

TIA
 
J

Jon Skeet [C# MVP]

C# Learner said:
I'm trying to convert a UTF8 string to an ASCII string, but the code
I've tried isn't working.

<code>

private static string Utf8ToAscii(string value)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(value);
byte[] asciiBytes = Encoding.Convert(Encoding.UTF8,
Encoding.ASCII, utf8Bytes);

return Encoding.ASCII.GetString(asciiBytes);
}

</code>

Am I doing something wrong?

I'm not sure what you really want to be doing - strings themselves are
neither UTF-8 nor ASCII. If you just want to make sure that your string
only contains ASCII characters, I wouldn't do it that way - I'd convert
it to a char array and walk through the array making sure that all the
characters are < 128 and replacing them with '?' if they're not.

What are you actually trying to do?
 
C

C# Learner

What are you actually trying to do?

I'm reading data from a socket, and this data eventually ends up in a
string. The data has been previously encoded in UTF8 format (as
dictated by the communication protocol). I need to decode the string.

In Delphi I would simply call a library function called "Utf8ToAnsi"
which would do the job. I would like to know how to do this using the
..net framework.

Thanks
 
J

Jon Skeet [C# MVP]

C# Learner said:
I'm reading data from a socket, and this data eventually ends up in a
string. The data has been previously encoded in UTF8 format (as
dictated by the communication protocol). I need to decode the string.

If it's as a string now, the decoding should already have been done. If
it hasn't, you should have a byte array instead. Either do the decoding
when you get the data from the socket, or keep the data as binary until
you're in a position to decode it properly. You should never be turning
binary data into character data unless you know what encoding it's in.
 

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