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,325 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

27 Experts available now in Live!

Get 1:1 Help Now