1

How to turn such a string into an emoji? 1F600 => 😀 or 1F600 => \U0001F600 or 1F600 => 0x1F600

I spent a few days but I still didn't understand how to translate a string like 1F600 into emoji

2 Answers 2

3

You simply need to convert the value to the code point then get the character at that code point:

var emoji = Char.ConvertFromUtf32(Convert.ToInt32("1F600", 16));

Demo on dotnetfiddle

1
  • Much easier than my answer; I wasn't aware of Char.ConvertFromUtf32. Commented Nov 22, 2022 at 7:00
1

The string "1F600" is the hexadecimal representation of a Unicode code point. As it is not in the BMP, you either need UTF32 or a UTF16 surrogate pair to represent it.

Here is some code to perform the requested conversion using UTF32 representation:

  1. Parse as 32-bit integer:

    var utf32Char = uint.Parse("1F600", NumberStyles.AllowHexSpecifier);

  2. Convert this to a 4-element byte array in litte-endian byte order:

    var utf32Bytes = BitConverter.GetBytes(utf32Char);
    if (!BitConverter.IsLittleEndian)
        Array.Reverse(utf32Bytes);
    
  3. Finally, use Encoding.UTF32 to make a string from it.

    var str = Encoding.UTF32.GetString(utf32Bytes);
    Console.WriteLine(str);
    
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.