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

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

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:

Cast and use composite formatting:

char ch = 'A'; string strOut = String.Format(@"\x{0:x4}", (ushort)ch); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!