Search code examples
c#encodeiso-8859-1

C# convert ISO-8859-1 characters to entity number


I can't seem to figure out how to convert ISO-8859-1 characters, such as é, to it's entity number being é.

I want to be able to take a string, such as: "Steel Décor"

and have it converted to: "Steel Décor"


Solution

  • Assuming you don't care about HTML-encoding characters that are special in HTML (e.g., <, &, etc.), a simple loop over the string will work:

    string input = "Steel Décor";
    StringBuilder output = new StringBuilder();
    foreach (char ch in input)
    {
        if (ch > 0x7F)
            output.AppendFormat("&#{0};", (int) ch);
        else
            output.Append(ch);
    }
    // output.ToString() == "Steel D&#233;cor"
    

    The if statement may need to be changed to also escape characters < 0x20, or non-alphanumeric, etc., depending on your exact needs.