Java实现 中文转换成Unicode编码 和 Unicode编码转换成中文

想要实现中文字符转换为Unicode编码的话主要用到的是一个这样的包,自己可以去API文档里面查看下的

java.util.Properties; 

 

直接进入主题吧,主要是

 1 package Test01;
 2 
 3 import java.util.Properties;    
 4 public class Test {            
 5     public static void main(String[] args) {            
 6         String s = "简介";           
 7         String tt = gbEncoding(s);          
 8         System.out.println(decodeUnicode("\\u7b80\\u4ecb"));      
 9         System.out.println(HTMLDecoder.decode("开始"));         
10         String s1 = "\u7b80\u4ecb";          
11         System.out.println(s.indexOf("\\"));         
12         }         
13     public static String gbEncoding(final String gbString) {         
14         char[] utfBytes = gbString.toCharArray();               
15         String unicodeBytes = "";                
16         for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {  
17             String hexB = Integer.toHexString(utfBytes[byteIndex]);                       
18             if (hexB.length() <= 2) {                           
19                 hexB = "00" + hexB;                     
20                 }                       
21             unicodeBytes = unicodeBytes + "\\u" + hexB;                   
22             }                   
23         System.out.println("unicodeBytes is: " + unicodeBytes);                   
24         return unicodeBytes;            }                     
25     public static String decodeUnicode(final String dataStr) {                
26         int start = 0;                  
27         int end = 0;                
28         final StringBuffer buffer = new StringBuffer();                  
29         while (start > -1) {                     
30             end = dataStr.indexOf("\\u", start + 2);                      
31             String charStr = "";                      
32             if (end == -1) {                          
33                 charStr = dataStr.substring(start + 2, dataStr.length());                     
34                 } else {                        
35                     charStr = dataStr.substring(start + 2, end);                      
36                     }                     
37             char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。                    
38             buffer.append(new Character(letter).toString());                    
39             start = end;                  
40             }                  
41         return buffer.toString();             
42         }         
43     }  

 

转载于:https://www.cnblogs.com/Mr-stockings/p/8184750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值