emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)
前幾天剛好有需求要把emoji對(duì)應(yīng)的Unicode編碼轉(zhuǎn)換成文字,比如1f601對(duì)應(yīng)的這個(gè)笑臉😁,但沒(méi)有找到C#的把1f601轉(zhuǎn)換成文字的方法,用Encoding.Unicode怎么轉(zhuǎn)換都不對(duì),最后直接復(fù)制emoji字符,Visual Studio里面竟然直接顯示出來(lái)了,那就直接用字符吧,都不用轉(zhuǎn)換了,然后不了了之了。
今天搞Markdown編輯器,由于前面GFM的原因,又對(duì)編碼進(jìn)行測(cè)試,沒(méi)查到什么靠譜資料,到時(shí)找到很多emoji和Unicode對(duì)照表,https://apps.timwhitlock.info/emoji/tables/unicode拿一個(gè)笑臉https://apps.timwhitlock.info/unicode/inspect/hex/1F601開(kāi)刀~
1.表情字符轉(zhuǎn)編碼【C#】
Encoding.UTF32.GetBytes('😁') -> ['1', 'f6', '1', '0']
【js】
'😁'.codePointAt(0).toString(16) -> 1f601
【java】
byte[] bytes = '😀'.getBytes('utf-32'); System.out.println(getBytesCode(bytes)); private static String getBytesCode(byte[] bytes) { String code = ''; for (byte b : bytes) { code += 'x' + Integer.toHexString(b & 0xff); } return code; }
UTF-32結(jié)果一致
【C#】
Encoding.UTF8.GetBytes('😁') -> ['f0', '9f', '98', '81']
【js】
encodeURIComponent('😁') -> %F0%9F%98%81
UTF-8結(jié)果一致
2.編碼轉(zhuǎn)表情字符【js】
String.fromCodePoint(’0x1f601’) utf-32
【java】
String emojiName = '1f601'; //其實(shí)4個(gè)字節(jié) int emojiCode = Integer.valueOf(emojiName, 16); byte[] emojiBytes = int2bytes(emojiCode); String emojiChar = new String(emojiBytes, 'utf-32'); System.out.println(emojiChar); public static byte[] int2bytes(int num){ byte[] result = new byte[4]; result[0] = (byte)((num >>> 24) & 0xff);//說(shuō)明一 result[1] = (byte)((num >>> 16)& 0xff ); result[2] = (byte)((num >>> 8) & 0xff ); result[3] = (byte)((num >>> 0) & 0xff ); return result; }c# 漢字和Unicode編碼互相轉(zhuǎn)換實(shí)例
/// <summary>/// <summary>/// 字符串轉(zhuǎn)Unicode/// </summary>/// <param name='source'>源字符串</param>/// <returns>Unicode編碼后的字符串</returns>public static string String2Unicode(string source){ byte[] bytes = Encoding.Unicode.GetBytes(source); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < bytes.Length; i += 2) { stringBuilder.AppendFormat('u{0}{1}', bytes[i + 1].ToString('x').PadLeft(2, ’0’), bytes[i].ToString('x').PadLeft(2, ’0’)); } return stringBuilder.ToString();} /// <summary>/// Unicode轉(zhuǎn)字符串/// </summary>/// <param name='source'>經(jīng)過(guò)Unicode編碼的字符串</param>/// <returns>正常字符串</returns>public static string Unicode2String(string source){ return new Regex(@'u([0-9A-F]{4})', RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result('$1'), 16)));}參考地址:
https://www.jianshu.com/p/8a416537deb3
https://blog.csdn.net/a19881029/article/details/13511729
https://apps.timwhitlock.info/emoji/tables/unicode
到此這篇關(guān)于emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)的文章就介紹到這了,更多相關(guān)emoji表情與unicode編碼互轉(zhuǎn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作2. JavaWeb Servlet中url-pattern的使用3. Java 如何從spring容器中獲取注入的bean對(duì)象4. 原生JavaScript寫(xiě)出Tabs標(biāo)簽頁(yè)的實(shí)例代碼5. IntelliJ IDEA : .java文件左下角顯示"J"圖標(biāo)的問(wèn)題6. java將m3u8格式轉(zhuǎn)成視頻文件的方法7. IntelliJ IDEA 2021.1 EAP 1 發(fā)布支持 Java 16 和 WSL 28. Java14發(fā)布了,再也不怕NullPointerException了9. java 優(yōu)雅關(guān)閉線程池的方案10. JavaScript canvas實(shí)現(xiàn)文字時(shí)鐘
