0

I need to convert a string i.e. "hi" to & #104;& #105; is there a simple way of doing this? Here is a website that does what I need. http://unicode-table.com/en/tools/encoder/

Andy
  • 21
  • 5

3 Answers3

3

Try this:

var s = "hi";
var ss = String.Join("", s.Select(c => "&#" + (int)c + ";"));
ycsun
  • 1,785
  • 1
  • 12
  • 20
0

Try this:

string myString = "Hi there!";
string encodedString = myString.Aggregate("", (current, c) => current + string.Format("&#{0};", Convert.ToInt32(c)));
Icemanind
  • 45,770
  • 48
  • 167
  • 283
  • Fancy, but one should not build long string with string concatenation... If rebuilding `String.Join` with `Enumerable.Aggregate` you should still use `StringBuilder`... – Alexei Levenkov Sep 18 '15 at 02:07
0

Based on the answer to this question:

    static string EncodeNonAsciiCharacters(string value)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char c in value)
        {
            string encodedValue = "&#" + ((int)c).ToString("d4"); // <------- changed
            sb.Append(encodedValue);
        }
        return sb.ToString();
    }
Community
  • 1
  • 1
Mark Feldman
  • 14,952
  • 2
  • 28
  • 54