convert UTF-8 HEX into emoji representation

2019-09-07 01:29发布

问题:

How do we convert UTF-8 HEX: EE 94 93 into equivalent Emoji representation: 1f1e8, 1f1f3

taken from here: http://www.iemoji.com/view/emoji/175/places/regional-indicator-symbol-letters-cn

回答1:

public static string ConvertEmoji2UnicodeHex(string emoji)
        {
            if (string.IsNullOrWhiteSpace(emoji))
                return emoji;

            byte[] bytes = Encoding.UTF8.GetBytes(emoji);
            string firstItem = Convert.ToString(bytes[0], 2);  

            int iv;
            if (bytes.Length == 1)
            {
                iv = Convert.ToInt32(firstItem, 2);
            }
            else
            {
                StringBuilder sbBinary = new StringBuilder();
                sbBinary.Append(firstItem.Substring(bytes.Length + 1).TrimStart('0'));
                for (int i = 1; i < bytes.Length; i++)
                {
                    string item = Convert.ToString(bytes[i], 2);
                    item = item.Substring(2);
                    sbBinary.Append(item);
                }

                iv = Convert.ToInt32(sbBinary.ToString(), 2);
            }

            return Convert.ToString(iv, 16).PadLeft(4, '0');
        }


标签: c# utf-8 hex emoji