Convert UTF-8 string To ASCII using C#

Ever wanted to convert a string from UTF-8 to ASCII in order to use it in a text file or to post it on another on another website? Below you will find a static function that you can use from anyware in your Visual Studio solution to convert a string from UTF-8 to ASCII. It takes as a parameter the UTF-8 string and return the same string in ASCII.

public static string UTF8toASCII(string text)
{
	System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
	Byte[] encodedBytes = utf8.GetBytes(text);
	Byte[] convertedBytes =
			Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes);
	System.Text.Encoding ascii = System.Text.Encoding.ASCII;

	return ascii.GetString(convertedBytes);
}

4 Comments

  1. thanks a lot, i was looking for some thing like this.

  2. all of latin america and europe likes this post :mrgreen:

  3. I need to find out what encoding a string is using…

    Do you know how to do that???

  4. Amazing! Its in fact awesome paragraph, I have got much clear idea regarding from this post.

Leave a Reply

Your email address will not be published. Required fields are marked *