android ios emoji表情,emoji表情android,ios适配转换(一)

博客展示了Emoji表情与Unicode编码相互转换的算法。通过代码实现了将Emoji转换为Unicode编码,以及将Unicode编码还原为Emoji的过程。还提到使用正则表达式抓取特殊标记的表情,以实现接收时正确显示表情,发送部分待后续完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class EmojiCharacter {

// high offset

public static final int W1 = Integer.valueOf("D7C0",16);

// low offset

public static final int W2 = Integer.valueOf("DC00",16);

public static void main(String[] args) {

// ��的二进制字符串表示//16|2字符串

String doughnutBinary = Integer.toBinaryString(Integer.valueOf("1F604",16));

System.out.println(doughnutBinary);

// 拆成高低 10 位表示

String highSetsBinary = doughnutBinary.substring(0, doughnutBinary.length() - 10);

String lowSetsBinary = doughnutBinary.substring(doughnutBinary.length() - 10, doughnutBinary.length());

System.out.println(highSetsBinary); // 1111100

System.out.println(lowSetsBinary); // 1100000000

// 分别与偏移量相加,得到两个编码值

String highSetsHexFixed = Integer.toHexString(W1 + Integer.valueOf(highSetsBinary, 2));

String lowSetsHexFixed = Integer.toHexString(W2 + Integer.valueOf(lowSetsBinary, 2));

System.out.println(highSetsHexFixed); // d83c

System.out.println(lowSetsHexFixed); // df00//10进制转二进制

// 拼接这两个编码值,还原字符表示

char highChar = (char)Integer.valueOf(highSetsHexFixed, 16).intValue();//16进制转十进制

char lowChar = (char)Integer.valueOf(lowSetsHexFixed, 16).intValue();

System.out.println(highChar); // ?

System.out.println(lowChar); // ?

System.out.println(highChar + "" + lowChar); // ��

}

}

通过注释可以看出来一步步转换的步骤,下面是我自己经过反推出来的根据表情转换成Unicode编码的方法

public static void decompilation{

System.out.println(Integer.toHexString(highChar));

System.out.println(Integer.toHexString(lowChar));

System.out.println((Integer.parseInt(Integer.toHexString(highChar),16)-W1));

System.out.println((Integer.parseInt(Integer.toHexString(lowChar),16)-W2));

System.out.println(Integer.toBinaryString((Integer.parseInt(Integer.toHexString(highChar),16)-W1)) );

System.out.println(Integer.toBinaryString((Integer.parseInt(Integer.toHexString(lowChar),16)-W2)) );

System.out.println(Integer.toBinaryString((Integer.parseInt(Integer.toHexString(highChar),16)-W1)) +Integer.toBinaryString((Integer.parseInt(Integer.toHexString(lowChar),16)-W2)) );

System.out.println(Integer.toHexString(Integer.valueOf(Integer.toBinaryString((Integer.parseInt(Integer.toHexString(highChar),16)-W1)) +Integer.toBinaryString((Integer.parseInt(Integer.toHexString(lowChar),16)-W2)) , 2)));

}

经过这个转换得出的是类似1F604这样的16进制的数,因为IOS那里需要的是0x1f604这种格式的所以要自己添一下0x了,到了这里就可以和IOS同步了接受的时候调用第一个把Unicode转成表情显示,发送的时候调用第二个把表情转成Unicode

经过以上的算法以后我发现新的问题来了就是输入的时候怎么把表情抓出来转换成Unicode发出去呢,还有接受时候怎么转成表情呢?

接受时候转成表情我是用的正则表达式把经过我特殊标记的表情抓出来

举例 :(我把所有表情都用#表情#这样的格式发送的因为表情会自带头0x的所以我抓取的是#0x和后面第一个#之间的值(emoji的Unicode编码的位数不一样所以不能直接确定位数))

public static void extract(){

Pattern pattern=Pattern.compile("#0x.+?#",Pattern.CASE_INSENSITIVE);

Matcher matcher=pattern.matcher("我要#0x1F604##2123123123#0x1F605#");

StringBuffer buffer=new StringBuffer();

while (matcher.find()) {

matcher.appendReplacement(buffer, send(matcher.group().replace("#", "")));

}

matcher.appendTail(buffer);

System.out.println(buffer.toString());

}

这样就可以把接受到的表情正确的显示到上面的那个项目中。 至于发送的明天写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值