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.

4
  • Possible duplicate of How to convert a UTF-8 string into Unicode? Commented Oct 22, 2016 at 13:49
  • You mean, you want the hexadecimal representation of each unicode character? Commented Oct 22, 2016 at 13:55
  • yes exactly @maf-soft Commented Oct 22, 2016 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. Commented Oct 22, 2016 at 14:14

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I'm using c# 4 and it seems this code needs c# 6 :(
Keep both versions of the string formatting. The future is with the interpolated strings.
Why not simply use (ushort)t instead of Convert.ToUInt16(t)?
That's also an option
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).
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.