I'm trying to Convert a string in to a series of unicode characters.
for expample : if I have a string that contains "Ñ", the unicode I want would be this "U+00D1".
Edit
thank you everyone for your time. What I wanted was the hexadecimal representative of the unicode character no the character itself encoded in unicode.
2 Answers
Try this:
string input = "nsa";
var result = input.Select(t => string.Format("U+{0:X4} ", Convert.ToUInt16(t))).ToList();
Or with nicer formatting (C# 6):
string input = "nsa";
var result = input.Select(t => $"U+{Convert.ToUInt16(t):X4} ").ToList();
5 Comments
Henjin
Thank you. I'm using c# 4 and it seems this code needs c# 6 :(
Jeppe Stig Nielsen
Keep both versions of the string formatting. The future is with the interpolated strings.
Jeppe Stig Nielsen
Why not simply use
(ushort)t instead of Convert.ToUInt16(t)?Maor Veitsman
That's also an option
Jeppe Stig Nielsen
Note that if the string contains code points over U+FFFF, like
"abc\U00012345def" in C#, then this method will get the hex values of the surrogate pairs, not of the characters themselves. This may be exactly what you want, of course. If not, use the StringInfo class to iterate over the so-called text elements, and use the char.ConvertToUtf32 on the "text element" to get the numeric value(s).
charvalues which represent UTF-16 code units.