[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
Medium Priority
?
1,734 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

650 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