Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert C# characters to their hexadecimal code representation

Tags:

string

c#

.net

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"));
like image 272
Eli Avatar asked Dec 15 '08 18:12

Eli


1 Answers

Cast and use composite formatting:

char ch = 'A';
string strOut = String.Format(@"\x{0:x4}", (ushort)ch);
like image 70
user7116 Avatar answered Sep 30 '22 05:09

user7116