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

 

posted @   追极  阅读(21490)  评论(2编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示