PC Review


Reply
Thread Tools Rate Thread

Converting UTF8 -> ASCII

 
 
C# Learner
Guest
Posts: n/a
 
      20th Jan 2004
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Jan 2004
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
 
Reply With Quote
 
C# Learner
Guest
Posts: n/a
 
      21st Jan 2004
Jon Skeet [C# MVP] <(E-Mail Removed)> wrote:

<snip>

>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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Jan 2004
C# Learner <(E-Mail Removed)> wrote:
> >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.


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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting ASCII to UTF-8 Alci Microsoft ASP .NET 2 28th Nov 2007 04:27 PM
How to convert UTF8 data to a ASCII data? Hooyoo Microsoft C# .NET 1 5th Jan 2007 05:34 AM
converting ansi to utf8 format - is there anything wrong with it ? urgently requires help James Microsoft VB .NET 1 17th Mar 2006 11:02 AM
UTF8/UTF7/ASCII problem while reading from text file Lenard Gunda Microsoft C# .NET 5 7th Aug 2004 06:12 PM
Converting hex into ascii Randy Microsoft VC .NET 1 16th Mar 2004 09:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:44 AM.