2

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.

Henjin
  • 528
  • 2
  • 9
  • 19
  • Possible duplicate of [How to convert a UTF-8 string into Unicode?](http://stackoverflow.com/questions/11293994/how-to-convert-a-utf-8-string-into-unicode) – YuvShap Oct 22 '16 at 13:49
  • You mean, you want the hexadecimal representation of each unicode character? – maf-soft Oct 22 '16 at 13:55
  • yes exactly @maf-soft – Henjin Oct 22 '16 at 13:57
  • If I understand correctly, you are starting from a .NET string instance. That is not "a UTF-8 string". C# and .NET strings are based on `char` values which represent UTF-16 code units. – Jeppe Stig Nielsen Oct 22 '16 at 14:14

2 Answers2

5

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();
Maor Veitsman
  • 1,544
  • 9
  • 21
  • Thank you. I'm using c# 4 and it seems this code needs c# 6 :( – Henjin Oct 22 '16 at 13:57
  • 1
    Keep both versions of the string formatting. The future is with the interpolated strings. – Jeppe Stig Nielsen Oct 22 '16 at 14:11
  • 1
    Why not simply use `(ushort)t` instead of `Convert.ToUInt16(t)`? – Jeppe Stig Nielsen Oct 22 '16 at 14:16
  • That's also an option – Maor Veitsman Oct 22 '16 at 14:19
  • 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). – Jeppe Stig Nielsen Oct 22 '16 at 16:02
0

If you are not asking about the algorithm itself, then just use Encoding.Convert.

Marek Fekete
  • 641
  • 3
  • 16