3

I have a contact named Frédéric.

When I search it in contacts list in the site the name gets changed like this "Frédéric", Since it searches this name(Frédéric) in the database were I have the name Frédéric in it.

So there will not be a name like that in database it says no contacts found.

How do I get the real name other than this name Frédéric.?

my decode method is

public static string Decode(string text)
        {
            if (text == null)
            {
                return "";
            }

            string result = "";
            string[] values = text.Split('@');
            bool escaped = false;
            try
            {
                for (int i = 0; i < values.Length; i++)
                {
                    if (!escaped)
                    {
                        result += values[i].Replace("_"," ");
                    }
                    else
                    {
                        //result += System.Text.ASCIIEncoding.ASCII.GetChars(new byte[] { byte.Parse(values[i]) })[0];

                    }
                    escaped = !escaped;
                }
            }
            catch { }
            return result;
        }

here the argument of the method is Frédéric but it actually has to be Frédéric.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
saaswathy
  • 69
  • 2
  • 7

2 Answers2

10

You have a bug somewhere in your system where a UTF-8 encoded string is decoded using what is called the ANSI code page. This code demonstrates the bug:

var name = "Frédéric";
var bytes = Encoding.UTF8.GetBytes(name);
var wrongName = Encoding.Default.GetString(bytes);

Now wrongName is Frédéric.

What you need to do is this:

var name = "Frédéric";
var bytes = Encoding.UTF8.GetBytes(name);
var correctName = Encoding.UTF8.GetString(bytes);

The Encoding.Default varies depending on your Windows regional settings. On my computer the code page is Windows 1252 also known as ISO 8859:1 but in other parts of the world it may be another code page. I believe that the Japanese ANSI code page is 932 and in that case the wrong name will come out as Frテゥdテゥric.

Anyway, the correct encoding to use is UTF-8 because your string is encoded using that encoding. Trying to "repair" the mangled string is not a fruitful path because it depends on the ANSI code page of the system the code executes on.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • In principle he could use the default encoding for both instead of UTF-8 for both, but that's a really bad idea. UTF-8 is clearly preferable since it avoids the codepage pains. – CodesInChaos Sep 26 '12 at 08:56
  • hi martin, In my decode method i have done this thing result += System.Text.ASCIIEncoding.ASCII.GetChars(new byte[] { byte.Parse(values[i]) })[0]; – saaswathy Sep 26 '12 at 08:56
  • @saaswathy: To decode the bytes you need to use the same encoding as was used during the encoding. I have demonstrated that if you encode using UTF-8 and decode using ISO 8859:1 you get `Frédéric` which pretty much implies that you encode using UTF-8. This means that you also have to decode using UTF-8. E.g. call `Encoding.UTF8.GetString` to decode bytes. Dont' encode/decode using ASCII - `é` will then map to `?`. – Martin Liversage Sep 26 '12 at 09:11
  • @saaswathy. How is my answer not working? I just demonstrate how mixing UTF-8 and ISO 8859:1 changes `Frédéric` into `Frédéric` and you if you run the code in my answer you will see that my statement is correct. How to fix your code I don't know because you haven't provided information about how your strings are encocded and decoded. The function you have added to your question apparently is called with a string that already is mangled. You need to backtrack to where that string is created and then fix the problem at that point. – Martin Liversage Sep 26 '12 at 10:15
0

You can use HtmlEncode in order to encode your caracter

string encoded = HttpUtility.HtmlEncode("Frédéric");

Link (Special Caracter ): http://www.utexas.edu/learn/html/spchar.html

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51