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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
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…

825 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