How to convert a number (larger than 0xFFFF) that represents a unicode character to it's equivlent string in c#

Advertisements

help!

For example, a character ‘𠀀’ in CJK Unified Ideographs Extension A, It’s unicode value is 0x20000, as 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 it’s equivlent string like "𠀀"

>Solution :

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

Leave a ReplyCancel reply