wpf 如何将表情符号转换为其 UTF-32/转义的 unicode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44728740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert emoticons to its UTF-32/escaped unicode?
提问by Joker_37
I am working on a chatting application in WPF and I want to use emoticons in it. I am working on WPF app. I want to read emoticons which are coming from Android/iOS devices and show respective images.
我正在 WPF 中开发一个聊天应用程序,我想在其中使用表情符号。我正在开发 WPF 应用程序。我想阅读来自 Android/iOS 设备的表情符号并显示相应的图像。
On WPF, I am getting a black Emoticon looking like
. I somehow got a library of emoji icons which are saved with respective hex/escaped unicode values.
So, I want to convert these symbols of emoticons into UTF-32/escaped unicode so that I can directly replace related emoji icons with them.
在 WPF 上,我得到一个黑色的表情符号,看起来像
. 我不知何故得到了一个表情符号图标库,这些图标用各自的十六进制/转义的 unicode 值保存。所以,我想把这些表情符号转换成UTF-32/转义的unicode,这样我就可以直接用它们替换相关的表情符号。
I had tried to convert an emoticon to its unicode but end up getting a different string with couple of symbols, which are having different unicode.
我曾尝试将表情符号转换为其 unicode,但最终得到了一个不同的字符串,其中包含几个具有不同 unicode 的符号。
string unicodeString = "\u1F642"; // represents
Encoding unicode = Encoding.Unicode;
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
char[] unicodeChars = new char[unicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)];
unicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0);
string asciiString = new string(unicodeChars);
Any help is appreciated!!
任何帮助表示赞赏!
回答by Rand Random
Your escaped Unicode String is invalid in C#.
您转义的 Unicode 字符串在 C# 中无效。
string unicodeString = "\u1F642"; // represents
This piece of code doesnt represent the "slightly smiling face" since C# only respects the first 4 characters - representing an UTF-16 (with 2 Bytes).
这段代码不代表“微微笑的脸”,因为 C# 只尊重前 4 个字符 - 代表 UTF-16(2 个字节)。
So what you actually get is the letter representing 1F64followed by a simple 2.
http://www.fileformat.info/info/unicode/char/1f64/index.htm
所以你实际得到的是代表1F64后跟一个简单2.
http://www.fileformat.info/info/unicode/char/1f64/index.htm
So this: ?2
所以这: ?2
If you want to type hex with 4 Bytes and get the corresponding string you have to use:
如果你想用 4 个字节输入十六进制并获得相应的字符串,你必须使用:
var unicodeString = char.ConvertFromUtf32(0x1F642);
https://msdn.microsoft.com/en-us/library/system.char.convertfromutf32(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.char.convertfromutf32(v=vs.110).aspx
or you could write it like this:
或者你可以这样写:
\uD83D\uDE42
This string can than be parsed like this, to get your desired result which is again is the hex value that we started with:
这个字符串可以像这样解析,以获得您想要的结果,这也是我们开始时的十六进制值:
var x = char.ConvertFromUtf32(0x1F642);
var enc = new UTF32Encoding(true, false);
var bytes = enc.GetBytes(x);
var hex = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hex.AppendFormat("{0:x2}", bytes[i]);
}
var o = hex.ToString();
//result is 0001F642
(The result has the leading Zeros, since an UTF-32 is always 4 Bytes)
(结果有前导零,因为 UTF-32 总是 4 个字节)
Instead of the for Loop you can also use BitConverter.ToString(byte[])https://msdn.microsoft.com/en-us/library/3a733s97(v=vs.110).aspxthe result than will look like:
除了 for 循环,您还可以使用BitConverter.ToString(byte[])https://msdn.microsoft.com/en-us/library/3a733s97(v=vs.110).aspx结果如下所示:
var x = char.ConvertFromUtf32(0x1F642);
var enc = new UTF32Encoding(true, false);
var bytes = enc.GetBytes(x);
var o = BitConverter.ToString(bytes);
//result is 00-01-F6-42
回答by Jimbot
Please be aware that Encoding.Unicodeis UTF-16 in C#. To read 32 bits Unicode, there is this Encoding.UTF32. Link on MSDN for Encoding.?UT?F32
请注意,这Encoding.Unicode是 C# 中的 UTF-16。要读取 32 位 Unicode,有这个Encoding.UTF32. MSDN 上用于编码的链接。?UT?F32

