The Fallen The Fallen - 3 years ago 191
C# Question

Converting string to Unicode characters

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".


thank you everyone for your time. What I wanted was the hexadecimal representative of the unicode character no the character itself encoded in unicode.

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download