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,523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
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…

726 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