20

Possible Duplicate:
Converting Unicode strings to escaped ascii string

How can I convert ä... into something like \u0131... ?

is there any Function for doing this ?

p.s :

beside this way : [ sorry @Kendall Frey :-)]

char a = 'ä';
string escape = "\\u" + ((int)a).ToString("X").PadLeft(4, '0');
Community
  • 1
  • 1
Royi Namir
  • 138,711
  • 129
  • 435
  • 755

2 Answers2

28

Here's a function to convert a char to an escape sequence:

string GetEscapeSequence(char c)
{
    return "\\u" + ((int)c).ToString("X4");
}

It isn't gonna get much better than a one-liner.

And no, there's no built-in function as far as I know.

Kendall Frey
  • 41,292
  • 18
  • 105
  • 145
8

There is no built-in function AFAIK. Here is one pretty silly solution that works. But Kendall Frey provided much better variant.

string GetUnicodeString(string s)
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in s)
    {
        sb.Append("\\u");
        sb.Append(String.Format("{0:x4}", (int)c));
    }
    return sb.ToString();
}
ChruS
  • 3,647
  • 5
  • 28
  • 38