9

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"));
Eli
  • 1,295
  • 1
  • 15
  • 28

1 Answers1

14

Cast and use composite formatting:

char ch = 'A';
string strOut = String.Format(@"\x{0:x4}", (ushort)ch);
user7116
  • 61,730
  • 16
  • 140
  • 170