How to convert a number (larger than 0xFFFF) that represents a Unicode character to its equivalent string in C#

Viewed 172

For example, a character '' in CJK Unified Ideographs Extension A; its unicode value is 0x20000, as a char in C# can't represent such character, so I wonder if I could convert it to string, my question is:

If I give you a number like 0x20000, how to convert it and let me get its equivalent string like ""

1 Answers

You can use char.ConvertFromUtf32 for that:

int utf32 = 0x20000;
string text = char.ConvertFromUtf32(utf32);

string itself is a sequence of UTF-16 code units, in this case U+D840 and U+DC00, which you can see by printing out the individual char values:

int utf32 = 0x20000;
string text = char.ConvertFromUtf32(utf32);
Console.WriteLine(((int) text[0]).ToString("x4")); // d840
Console.WriteLine(((int) text[1]).ToString("x4")); // dc00
Related