emoji表情与unicode编码互转(JS,JAVA,C#)
1.表情字符转编码
【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结果一致
【C#】
Encoding.UTF8.GetBytes("😁")
->
["f0", "9f", "98", "81"]
【js】
encodeURIComponent("😁")
->
%F0%9F%98%81
UTF-8结果一致
2.编码转表情字符
【js】 String.fromCodePoint('0x1f601') utf-32
【java】
String emojiName = "1f601"; //其实4个字节 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);//说明一 result[1] = (byte)((num >>> 16)& 0xff ); result[2] = (byte)((num >>> 8) & 0xff ); result[3] = (byte)((num >>> 0) & 0xff ); return result; }
参考地址:
https://www.jianshu.com/p/8a416537deb3
https://blog.csdn.net/a19881029/article/details/13511729
https://apps.timwhitlock.info/emoji/tables/unicode