1

How can I convert 'Supplementary' Unicode code points (21-bit) to System Strings in .NET Framework 4.7.2? It seems that in .NET 5.0, the Char.ConvertFromUtf32() method handles this, but in .NET framework, this method can't handle values above the 'Basic Multilingual Plane' range. I need something like this:

Console.WriteLine(Char.ConvertFromUtf32(0x0001F381));

to produce this: .

I have tried:

int Gift = 0x0001F381;
string strGift = Char.ConvertFromUtf32(Gift);
Console.WriteLine($"Gift (0x{Gift:X}) is {strGift}.");

but it just gives me: Gift (0x1F381) is ??.

How can I code this to work in .NET framework 4.7.2?

JNygrenLT
  • 173
  • 12
  • 3
    It might be Console output encoding actually why you see '??', not because your Covert doesn't work. Maybe default output encoding is different on .NET Framework and .NET Core, try setting it manually - https://learn.microsoft.com/en-us/dotnet/api/system.console.outputencoding?redirectedfrom=MSDN&view=net-5.0#System_Console_OutputEncoding – Nikita Chayka Dec 15 '20 at 17:15
  • 1
    See following : https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.utf32?view=net-5.0 – jdweng Dec 15 '20 at 17:21
  • As @NikitaChayka said this code works perfectly fine in 4.x (and should work fine in any version starting from 2.0). It is extremely unlikely you get emoji printed in default console as in most cases fonts there don't support those. Using console that supports all symbols (i.e. in LinqPad) will produce result you expect. If you really want to get your local console to print those - https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console is good starting point (or LMGTFY - https://www.bing.com/search?q=c%23+console+unicode+question+mark) – Alexei Levenkov Dec 15 '20 at 17:53
  • @NikitaChayka, looks like you're right! I tried: string Sample = "A ☂ Z"; Console.WriteLine(Sample); and got: "A ? ?? Z". I'm following up on provided links. Thanks. – JNygrenLT Dec 15 '20 at 18:47
  • I 'oversimplified' my problem by trying to output to the console. "string strGift = Char.ConvertFromUtf32(Gift);" was giving me exactly what I needed. Thanks. – JNygrenLT Dec 15 '20 at 20:25

1 Answers1

1

The right answer to the question is to use "Char.ConvertFromUtf32".

By trying to show the result in the console, I created an entirely different problem.

JNygrenLT
  • 173
  • 12