convert a hex string to corresponding emoji string

10,078

Solution 1

You have a string containing two shorts in hexadecimal form, so you need to parse them first. My example uses an overload of Convert.ToInt16 which also accepts an integer specifying the base of the integers in the string which, in our case, is 16 (hexadecimal).

string ParseUnicodeHex(string hex)
{
    var sb = new StringBuilder();
    for (int i = 0; i < hex.Length; i+=4)
    {
        string temp = hex.Substring(i, 4);
        char character = (char)Convert.ToInt16(temp, 16);
        sb.Append(character);
    }
    return sb.ToString();
}

Please note that this method will fail if the string's length isn't divisible by 4.

The reason this works:

textbox.Text += "\uD83D\uDC71";

is because you've got a string literal containing unicode character escape sequences. When you compile your program, the compiler replaces these escape sequences with the correct unicode bytes. This is why you cannot just add \u in front of the characters during execution to make it work.

Solution 2

Okay. It seems you have a string which gives the hexadecimal of each of the UTF-16 code units of the character U+1F471 (👱).

Since char represents a UTF-16 code unit, split the string into two 4-character chunks, parse that into an int as hexadecimal, cast each to char and then combine them into a string:

var personWithBlondHair = ""
  + (char)int.Parse("D83DDC71".Substring(0, 4), NumberStyles.HexNumber)
  + (char)int.Parse("D83DDC71".Substring(4, 4), NumberStyles.HexNumber);

As per https://dotnetfiddle.net/oTgXfG

Share:
10,078
frenk91
Author by

frenk91

Windows Phone and Windows Developer

Updated on June 07, 2022

Comments

  • frenk91
    frenk91 almost 2 years

    I'm trying to create a string with emoji "👱" starting from this string "D83DDC71". For doing that I'm trying to convert the string above in this string "\uD83D\uDC71".

    If i use this code it work (textbox shows 👱 as expected):

    textbox.Text += "\uD83D\uDC71";
    

    but if i use this it doesn't work (textbox shows exact text "\uD83D\uDC71" instead of single character):

    textbox.Text += sender.Code.ToString("X").insert(4, @"\u").insert(0, @"\u");
    

    What is the right way to convert hex representation of an emoji to a corresponding C# string (UTF-16)?

  • Alexei Levenkov
    Alexei Levenkov almost 9 years
    What @"\u" + m.Value supposed to do?
  • M.kazem Akhgary
    M.kazem Akhgary almost 9 years
    ex : m.Value will be "D83D". then it will be "\uD83D" @AlexeiLevenkov
  • Alexei Levenkov
    Alexei Levenkov almost 9 years
    Have you seen the question? Which is basically "why "\uD83D\uDC71" produces emoji while @"\u"+ "d83d" + @"\" + "DC71" does not"... So not sure how your post answers it. Additionally \d not going to match letters, but that is less interesting.
  • Jon Hanna
    Jon Hanna almost 9 years
    No, it will be @"\uD83D" and you need "\uD83D". But it won't even be that because you are passing "" instead of str to the regular expression matching. Also the regular expression is looking for decimal digits, not hex digits.
  • M.kazem Akhgary
    M.kazem Akhgary almost 9 years
    @JonHanna that was typo. i try this. maybe i need to convert m.Value to unicode char. but i thought this may work
  • Jon Hanna
    Jon Hanna almost 9 years
    There are still no matches for \d{4} in str, and still no point adding @"\u" to things.
  • M.kazem Akhgary
    M.kazem Akhgary almost 9 years
    yes. it should be \S. i fixed it. however i used your algorithm to convert to char. @JonHanna
  • M.kazem Akhgary
    M.kazem Akhgary almost 9 years
    @AlexeiLevenkov see the edit. how ever using regex was just an idea but the point was to convert unicode string into char.
  • Alexei Levenkov
    Alexei Levenkov almost 9 years
    Looks correct to me now. I'd personally not use regex for such thing as splitting string into fixed size chunks but that completely opinion based :).