Is there any way to convert unicode values to ASCII?

7 Answers

To simply strip the accents from unicode characters you can use something like:

string.Concat(input.Normalize(NormalizationForm.FormD).Where( c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)); 
1

You CAN'T convert from Unicode to ASCII. Almost every character in Unicode cannot be expressed in ASCII, and those that can be expressed have exactly the same codepoints in ASCII as in UTF-8, which is probably what you have. Almost the only thing you can do that is even close to the right thing is to discard all characters above codepoint 128, and even that is very likely nowhere near what your requirements say. (The other possibility is to simplify accented or umlauted letters to make more than 128 characters 'nearly' expressible, but that still doesn't even begin to actually cover Unicode.)

0

Technically, yes you can by using Encoding.ASCII.

Example (from byte[] to ASCII):

// Convert Unicode to Bytes byte[] uni = Encoding.Unicode.GetBytes("Whatever unicode string you have"); // Convert to ASCII string Ascii = Encoding.ASCII.GetString(uni); 

Just remember Unicode a much larger standard than Ascii and there will be characters that simply cannot be correctly encoded. Have a look here for tables and a little more information on the two encodings.

3

This workaround might better suit your needs. It strips the unicode chars from a string and only keeps the ASCII chars.

byte[] bytes = Encoding.ASCII.GetBytes("eéêëèiïaâäàåcç  test"); char[] chars = Encoding.ASCII.GetChars(bytes); string line = new String(chars); line = line.Replace("?", ""); //Results in "eiac test" 

Please note that the 2nd "space" in the character input string is the char with ASCII value 255

Well, seeing as how there's some 100,000+ unicode characters and only 128 ASCII characters, a 1-1 mapping is obviously impossible.

You can use the Encoding.ASCII object to get the ASCII byte values from a Unicode string, though.

If your metadata fields only accept ASCII input. Unicode characters can be converted to their TEX equivalent through MathJax. What is MathJax? MathJax is a JavaScript display engine for rendering TEX or MathML-coded mathematics in browsers without requiring font installation or browser plug-ins. Any modern browser with JavaScript enabled will be MathJax-ready. For general information about MathJax, visit mathjax.org.

It depends what you mean by "convert".

You can transliterate using the AnyAscii package.

// C# using AnyAscii; string s = "άνθρωποι".Transliterate(); // anthropoi 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy