2

I'm trying to convert emoji's like the one below to c# code so I can put the code in a TreeView node or facebook or other social engine. I tried the code for airplane and shows a little airplane in the treenode. But I use another airplace code like U+1F6E9 it just shows a little rectangle not the emoji. Please help.

string tnt = "Airplane " + char.ConvertFromUtf32(int.Parse("U+2708".Substring(2), System.Globalization.NumberStyles.HexNumber));

MyTreeView.Nodes.Add(tnt);

GRF
  • 171
  • 2
  • 10
  • 3
    What do you mean by "doesn't work"? Do you get an error? Unexpected results? You need to be specific . – Daniel Mann May 11 '20 at 22:21
  • This might work. [link](https://stackoverflow.com/a/40950241/10470443) – Muhammed Caylak May 11 '20 at 22:22
  • 1
    First of all, [`U+1F347`](https://www.compart.com/en/unicode/U+1F347) is the Unicode for a grape emoji. The golf one is [`U+26F3`](https://www.compart.com/en/unicode/U+26F3). Second, your code outputs the target emoji just fine. What exactly is the problem? Please note that "doesn't work" isn't a good description of a problem. – 41686d6564 stands w. Palestine May 11 '20 at 22:24
  • What I mean, I need to use code to add an emoji icon to say a Treeview tree node or Facebook post using code derived from the unicode values shown on http://www.unicode.org/emoji/charts/full-emoji-list.html#1f6b4 in the Code column. – GRF May 11 '20 at 22:38
  • @GRF I tested your code and both emojis were [displayed in the TreeView](https://i.imgur.com/ejSnrh6.png). – 41686d6564 stands w. Palestine May 14 '20 at 02:22

1 Answers1

0

Here is a class you may use to implement different emoji's in your application.

public class Emoji
{
    readonly int[] codes;
    public Emoji(int[] codes)
    {
        this.codes = codes;
    }

    public Emoji(int code)
    {
        codes = new int[] { code };
    }

    public override string ToString()
    {
        if (codes == null)
            return string.Empty;

        var sb = new StringBuilder(codes.Length);

        foreach (var code in codes)
            sb.Append(Char.ConvertFromUtf32(code));

        return sb.ToString();
    }
}

I would use the codes from this site unicode.org Instead of using the code it has on the site which is something like 'U+1F366' I would use '0x1F366' to specify hexadecimal notation. Hope this helps.

Nick Juelich
  • 428
  • 2
  • 13
  • Yes. Using those codes from Unicode.org. How could I use your class to say add an emoji image to a Treeview node or a Facebook post? – GRF May 11 '20 at 22:34
  • This is no different than code provided in the question. Please clarify how this code solves "so I can put the code in a TreeView node or facebook or other social engine. I tried the code below but doesn't work. " – Alexei Levenkov May 11 '20 at 23:02
  • Nick, I tried the class above and put the airplane emoji as the image in a treeview node. But if use another airplane code, there is just a little square where the icon should be. Please advise `Emoji emoji = new Emoji(0x2708); // shows ✈` `string em = emoji.ToString();` `EmojiTreeView.Nodes.Add(em);` `Emoji emoji = new Emoji(0x1F6E9); // shows a tiny triangle` `em = emoji.ToString();` `EmojiTreeView.Nodes.Add(em);` – GRF May 12 '20 at 04:07