emoji表情與unicode編碼互轉(JS,JAVA,C#)

1.表情字符轉編碼

【C#】 Encoding.UTF32.GetBytes("😁") -> ["1", "f6", "1", "0"]
【js】 "😁".codePointAt(0).toString(16) -> 1f601java

【java】app

    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結果一致spa

 

2.編碼轉表情字符

【js】 String.fromCodePoint('0x1f601')   utf-32.net

【java】 code

    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/8a416537deb3blog

https://blog.csdn.net/a19881029/article/details/13511729
unicode

https://apps.timwhitlock.info/emoji/tables/unicodeget

相關文章
相關標籤/搜索