convert utf8 problem

  • Thread starter Thread starter dongxm
  • Start date Start date
dongxm said:
Is there a function can convert "abc" to "\u0097\u0098\u0099" in dotnet(c#)

Do you mean you want a string with the unicode escapes in? Note that
that isn't UTF-8, particuarly.

Note that "a" is \u0061 by the way - escape values are in hex, not
decimal.

This prints out the escaped version of the first command line parameter
you give it:

using System;
using System.Text;

public class foo
{
static void Main(string[] args)
{
StringBuilder builder = new StringBuilder(args[0].Length*6);
foreach (char c in args[0])
{
builder.Append("\\u");
builder.AppendFormat("{0:X4}", (int)c);
}
Console.WriteLine (builder.ToString());
}
}

See http://www.pobox.com/~skeet/csharp/unicode.html for more about
encodings, and http://www.pobox.com/~skeet/csharp/strings.html for more
about strings in general.
 
Back
Top