Results 1 to 6 of 6

Thread: [RESOLVED] How to convert emoji's unicode strings

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Resolved [RESOLVED] How to convert emoji's unicode strings

    Good Day All

    i have a unicode character which works very well

    /// Thinking face 🤔
    /// </summary>
    UnicodeString Thinking =0x1f914;

    as you can see the value here is "0x1f914"

    so i use this site which has a list of emoji's [Full Emoji List, v12.0](http://www.unicode.org/emoji/charts/...moji-list.html)

    but the code in that website is "U+1F914"

    how do i convert U+1F914 to 0x1f914 in c#

    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,292

    Re: How to convert emoji's unicode strings

    The '0x' is part of the code, not part of the value. It's just how hexadecimal literals are indicated in code, just like double quotes around text indicates a string literal and single quotes a char literal. If you want a string that contains "U+1F914" from that number then you would use something like this:
    csharp Code:
    1. var text = $"U+{number:X}";
    or:
    csharp Code:
    1. var text = "U+" + number.ToString("X");
    The 'X' format specifier indicates upper-case hexadecimal.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,292

    Re: How to convert emoji's unicode strings

    Hmmm... I think I may have misinterpreted the question and got it the wrong way around. Are you saying that you want to get a number from the text? If so then something like this:
    csharp Code:
    1. var number = Convert.ToInt32(text.Substring(2), 16);
    or:
    csharp Code:
    1. var number = int.Parse(text.Substring(2), NumberStyles.HexNumber);

  4. #4

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Re: How to convert emoji's unicode strings

    Good Day



    After i looked closely on the string , with lack of knowledge of the unicode stuff , i realized i just needed to replace the character and the emoji will be translated , the code below simply solved the problem


    Code:
            txtresults.Text = txttext.Text.Replace("U+", "0x").ToLower();

  5. #5

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Re: How to convert emoji's unicode strings

    Good Day



    After i looked closely on the string , with lack of knowledge of the unicode stuff , i realized i just needed to replace the character and the emoji will be translated , the code below simply solved the problem


    Code:
            txtresults.Text = txttext.Text.Replace("U+", "0x").ToLower();

  6. #6
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: [RESOLVED] How to convert emoji's unicode strings

    Hello , @vuyiswamb

    Please try this code,To convert emoji's unicode strings:
    Code:
    var convertStr = string.Join("-", Regex.Matches(res, @"..").Cast<Match>().ToList());
    String[] tempArr = convertStr.Split('-');
    byte[] decBytes = new byte[tempArr.Length];
    for (int i = 0; i < tempArr.Length; i++)
    {
        decBytes[i] = Convert.ToByte(tempArr[i], 16);
    }
    String str = Encoding.BigEndianUnicode.GetString(decBytes);
    I hope this code will be usefull for you.
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width