How to convert emoticons to its UTF-32/escaped unicode? How to convert emoticons to its UTF-32/escaped unicode? wpf wpf

How to convert emoticons to its UTF-32/escaped unicode?


Your escaped Unicode String is invalid in 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).

So what you actually get is the letter representing 1F64 followed by a simple 2.http://www.fileformat.info/info/unicode/char/1f64/index.htm

So this: ὤ2

If you want to type hex with 4 Bytes and get the corresponding string you have to use:

var unicodeString = char.ConvertFromUtf32(0x1F642);

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)

Instead of the for Loop you can also use BitConverter.ToString(byte[]) https://msdn.microsoft.com/en-us/library/3a733s97(v=vs.110).aspx the result than will look like:

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


Please be aware that Encoding.Unicode is UTF-16 in C#. To read 32 bits Unicode, there is this Encoding.UTF32. Link on MSDN for Encoding.​UT​F32