emoji表情等特殊字符处理和存储的两个方案

方案1.改数据库配置

使之支持emoji表情等特殊字符,小公司或者个人开发还好,大公司用此方案代价较大。

以mysql为例,改配置方法参考:https://blog.csdn.net/u010737354/article/details/52456668

 

方案2.使用unicode编码(推荐使用)

统一标准:入库或入缓存统一对特殊字符unicode编码,从库或缓存取出后统一进行unicode编码

(1)unicode加密:

//加密emoji表情
	public static String encodeToUnicode(String string) {
	    StringBuffer unicode = new StringBuffer();  
	   
	    for (int i = 0; i < string.length(); i++) {  
	   
	        // 取出每一个字符  
	        char c = string.charAt(i);  
	   
	        // 转换为unicode  
	        unicode.append("\u" + Integer.toHexString(c));  
	    }  
	   
	    return unicode.toString();  
	}
(2)unicode解密:

//解密emoji表情
	public static String decodeFromUnicode(String unicode) {
	    StringBuffer string = new StringBuffer();  
	   
	    String[] hex = unicode.split("\\u");  
	    if(hex.length==1){
	    	return unicode;
	    }
	   
	    for (int i = 1; i < hex.length; i++) {  
	   
	        // 转换出每一个代码点  
	        int data = Integer.parseInt(hex[i], 16);  
	   
	        // 追加成string  
	        string.append((char) data);  
	    }  
	   
	    return string.toString();  
	}
 

  

emoji表情等特殊字符处理和存储的两个方案_透明大脑-CSDN博客_emoji表情存储

原文地址:https://www.cnblogs.com/q1359720840/p/14090878.html