10

What I need to do is convert a C# character to an escaped unicode string:

So, 'A' - > "\x0041".

Is there a better way to do this than:

char ch = 'A';
string strOut = String.Format("\\x{0}", Convert.ToUInt16(ch).ToString("x4"));

1 Answer 1

15

Cast and use composite formatting:

char ch = 'A';
string strOut = String.Format(@"\x{0:x4}", (ushort)ch);
1
  • I didn't know that was called composite formatting. Thanks! +1
    – Greg
    Commented Dec 15, 2008 at 18:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.