如何在Xamarin.forms中将表情符号从转换为unicode? [英] How to convert emojis from to unicode in Xamarin.forms?

查看:188
本文介绍了如何在Xamarin.forms中将表情符号从转换为unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Xamarin.Forms项目.我在其中有文本框,并有一个按钮,可从文本框中获取文本并将其传递给API进行存储.现在的关键是,当用户从键盘上选择任何表情符号时,我想获取表情符号的unicode字符.目前,当我检查它的Text属性时,它会自我获得表情符号.

I have Xamarin.Forms project. I have textbox in that and have a button which get text from textbox and pass it to API to store. Now the point is when user select any emojis from keyboard, I want to get unicode character of the emojis. Currently I am getting emojis it self when I check Text property of it.

我想从Text属性中获取NewTextValue中给定的Unicode而不是表情符号.

I want to get Unicode rather emoji as given in NewTextValue from Text property.

这篇文章是一样的,但我不知道他是如何做到的. POST

This post is same but I don't understand how the guy has managed. POST

请提出建议.

一些Google之后,我尝试了以下操作.

After some google, I have tried with following.

string res = BitConverter.ToString(Encoding.BigEndianUnicode.GetBytes(str)).Replace("-", "");

这是结果res = D83DDE00 我不知道上面的代码是否是unicode.

This is result res = D83DDE00 I don't know above code is unicode or not.

如何转换回原始表情符号,或者还有其他方法可以用unicode转换吗?

How can I convert back to original emoji or is there any other way to convert in unicode?

推荐答案

我们需要手动将其转换回来.每两个字符插入-":

We need to manually convert it back. Insert "-" every two characters:

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);

此外,在我的测试中,Encoding.UTF32.GetBytes()可能更接近表情符号代码.您可以使用\U0001F600对其进行测试,这是一个微笑图像.用utf32转换后,字节仅改变其顺序.

Moreover in my test, Encoding.UTF32.GetBytes() may be closer to emoji code. You can test it with \U0001F600, this is a smile image. After converting with utf32, the bytes just change its order.

这篇关于如何在Xamarin.forms中将表情符号从转换为unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆