Solved

How can I convert emoji to hex in vb.net or C#? (I've included a working example in Javascript)

Posted on 2014-12-11
2
1,265 Views
Last Modified: 2014-12-19
Hi experts!

I'm looking to convert input that contains emoji to show an image for the emoji based on the hex code. I've done this in Javascript using this source as a reference, though now I'm looking to do the same in C# or vb.net.

Here's what works in Javascript:
function displayEmoji(sInput) {
    var output = '';
    var chunks = sInput.split(/([\uD800-\uDBFF][\uDC00-\uDFFF])/);
    for (var i = 0, l = chunks.length; i < l; i++) {
        if (i % 2 == 0) {
            // Even-numbered chunks are plain text
            output = output + chunks[i];
        }
        else {
            // Odd-numbered chunks are surrogate pairs
            // We have TWO characters, but we want one codepoint; this is how you decode UTF-16
            var pair = chunks[i];
            var codepoint = (
                0x10000
                | ((pair.charCodeAt(0) - 0xD800) << 10)
                | (pair.charCodeAt(1) - 0xDC00)
            );
            var hex = codepoint.toString(16);  // now it's in hex
            output = output + "<img src=\"" + "https://abs.twimg.com/emoji/v1/72x72/" + hex + ".png\" style=\"height:1.5em; width:1.5em;\">";
        }
    }
    return output;
}

Open in new window


Can you show me how to do this in C# or Vb.net? You're help is greatly appreciated!
0
Comment
Question by:Drevo
2 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40495752
Here is C# code to replace the surrogate pairs with an image:
string DisplayEmoji(string input)
{
    StringBuilder output = new StringBuilder();
    var enumerator = StringInfo.GetTextElementEnumerator(input);
    while(enumerator.MoveNext()){
        string chunk = enumerator.GetTextElement();
        if (char.IsSurrogatePair(chunk, 0))
            output.Append("<img src=\"" + "https://abs.twimg.com/emoji/v1/72x72/" + char.ConvertToUtf32(chunk, 0).ToString("x") + ".png\" style=\"height:1.5em; width:1.5em;\">");
        else
            output.Append(chunk);
    }
    return output.ToString();
}

Open in new window

1
 

Author Closing Comment

by:Drevo
ID: 40509486
This is perfect! You made my day! Thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
help with issues with ReportViewer in VS2015 5 23
Long URL in SMS 6 24
C# TextBox 11 0
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now