Take the 2-minute tour ×

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"));
share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

Cast and use composite formatting:

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

Your Answer


