tags:

views:

89

answers:

3

From my previous question, http://stackoverflow.com/questions/3571563/converting-chinese-character-to-unicode, I had a good answer but with some code I didn't understand:

Console.WriteLine("U+{0:x4}", (int)myChar);

Could anyone explain this?

+1  A: 

That will simply create the literal string "U+1234"... now if you are wanting to convert a unicode code point into a char, you want Convert.ToChar(myChar)

http://msdn.microsoft.com/en-us/library/3hkfdkcx.aspx

Chris
+1  A: 

The 0 indicates which positional argument to substitute. The x displays a hexadecimal number, the 4 has it display four digits.

For example, the character ȿ (LATIN SMALL LETTER S WITH SWASH TAIL, codepoint 575) is printed as U+023F since 57510 = 23F16.

John Kugelman
Precise, thanks!
Mass
+4  A: 
Console.WriteLine("U+{0:x4}", (int)myChar);

is the equivalent to the call:

Console.WriteLine("U+{0}", ((int)myChar).ToString("x4"));

In a format string, the : indicates that the item should be displayed using the provided format. The x4 part indicates that the integer should be printed in its hexadecimal form using 4 characters. Refer to standard numeric format strings for more information.

Jeff M
Just saw this answer after I accepted the other one, but this one is very nice too!
Mass
I like Jeff's answer better than mine, to be honest!
John Kugelman