C# Learner <(E-Mail Removed)> wrote:
> 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?
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too