21

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
  • 144,742
  • 138
  • 468
  • 792

2 Answers2

30

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
  • 43,130
  • 20
  • 110
  • 148
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,707
  • 5
  • 29
  • 40