0

I have a problem with replaceing non-ASCII characters to their ASCII equivalent.

I can do that simply in Windows Forms, using something like this:

Encoding.UTF8.GetString(Encoding.GetEncoding("Cyrillic").GetBytes("Crazy text with żźćńąśłęó and other special characters"));

and i will receive something like this:

Crazy text with zzcnasleo and other special characters

Problem is, that in Silverlight I do not have Encodings like "Cyrillic" - I can use only four of them:

utf-8 - UTF8Encoding
utf-16 - UnicodeEncoding (little-endian)
utf-16BE - UnicodeEncoding (big-endian)
utf-16LE - UnicodeEncoding (little-endian)

Do you know how to achive this same result in C# Silverlight?

Daniel Perez
  • 4,292
  • 4
  • 29
  • 54
Marek
  • 49
  • 1
  • 7
  • Take a look at these questions: [ASCIIEncoding In Windows Phone 7](http://stackoverflow.com/questions/4022281/asciiencoding-in-windows-phone-7) and [Encoding.ASCII not available in Silverlight is there a alternative?](http://stackoverflow.com/questions/10010840/encoding-ascii-not-available-in-silverlight-is-there-a-alternative). – Abbas Jul 22 '14 at 08:53
  • Example in this question replacing special characters with question mark - that's not, what I'm looking for :-( – Marek Jul 22 '14 at 09:05
  • And the other answers from that question? Otherwise create your own converter? – Abbas Jul 22 '14 at 10:29
  • @Abbas: None of them do what I expect. I wrote my own method, which I added as an answer to post. – Marek Jul 23 '14 at 13:10

1 Answers1

0

I wrote my own converter, but which isn't so cool:

private string Convert(string value)
{
    string[] nonAsciiCharacters = new string[] { "À", "Á", "Â", "Ã", "Å", "Ä", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ñ", "Ò", "Ó", "Ô", "Ö", "Õ", "Ù", "Ú", "Û", "Ü", "Ý", "à", "á", "â", "ã", "ä", "å", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ý", "ÿ", "Ā", "ā", "Ă", "ă", "Ą", "ą", "Ć", "ć", "Ĉ", "ĉ", "Ċ", "ċ", "Č", "č", "Ď", "ď", "Đ", "đ", "Ē", "ē", "Ĕ", "ĕ", "Ė", "ė", "Ę", "ę", "Ě", "ě", "Ĝ", "ĝ", "Ğ", "ğ", "Ġ", "ġ", "Ģ", "ģ", "Ĥ", "ĥ", "Ĩ", "ĩ", "Ī", "ī", "Ĭ", "ĭ", "Į", "į", "İ", "ı", "Ĵ", "ĵ", "Ķ", "ķ", "ĸ", "Ĺ", "ĺ", "Ļ", "ļ", "Ľ", "ľ", "Ŀ", "ŀ", "Ł", "ł", "Ń", "ń", "Ņ", "ņ", "Ň", "ň", "ʼn", "Ŋ", "ŋ", "Ō", "ō", "Ŏ", "ŏ", "Ő", "ő", "Ŕ", "ŕ", "Ŗ", "ŗ", "Ř", "ř", "Ś", "ś", "Ŝ", "ŝ", "Ş", "ş", "Š", "š", "Ţ", "ţ", "Ť", "ť", "Ũ", "ũ", "Ū", "ū", "Ŭ", "ŭ", "Ů", "ů", "Ű", "ű", "Ų", "ų", "Ŵ", "ŵ", "Ŷ", "ŷ", "Ÿ", "Ź", "ź", "Ż", "ż", "Ž", "ž", "Ơ", "ơ", "Ư", "ư", "Ǎ", "ǎ", "Ǐ", "ǐ", "Ǒ", "ǒ", "Ǔ", "ǔ", "Ǖ", "ǖ", "Ǘ", "ǘ", "Ǚ", "ǚ", "Ǜ", "ǜ", "Ǻ", "ǻ", "Ǿ", "ǿ" };
    string[] asciiCharacters = new string[] { "A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "N", "O", "O", "O", "O", "O", "U", "U", "U", "U", "Y", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "n", "o", "o", "o", "o", "o", "o", "u", "u", "u", "y", "y", "A", "a", "A", "a", "A", "a", "C", "c", "C", "c", "C", "c", "C", "c", "D", "c", "D", "d", "E", "e", "E", "e", "E", "e", "E", "e", "E", "e", "G", "g", "G", "g", "G", "g", "G", "g", "H", "h", "I", "i", "I", "i", "I", "i", "I", "i", "I", "i", "J", "j", "K", "k", "k", "L", "l", "L", "l", "L", "l", "L", "l", "L", "l", "N", "n", "N", "n", "N", "n", "n", "N", "n", "O", "o", "O", "o", "O", "o", "R", "r", "R", "r", "R", "r", "S", "s", "S", "s", "S", "s", "S", "s", "T", "t", "T", "t", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "W", "w", "Y", "y", "Y", "Z", "z", "Z", "z", "Z", "z", "O", "o", "U", "u", "A", "a", "I", "i", "O", "o", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "A", "a", "O", "o" };

    value = Regex.Replace(name.Trim(), @"[\s]", "_"); // Replace white with under character

    for (int currentChar = 0; currentChar  < nonAsciiCharacters.Length; currentChar ++)
    {
        value = value.Replace(nonAsciiCharacters[i], asciiCharacters[currentChar ]);
    }

    return Regex.Replace(value, @"[^0-9a-zA-Z_]+", string.Empty); // Remove white characters
}

Maybe somebody will make it better and put at this post :-)

Marek
  • 49
  • 1
  • 7