commons-codec

commons codec 提供 base64, hex, 及 metaphone, soundex 等编码演算。

下载地址:http://commons.apache.org/codec/

A.Base64 编解码

 

  1. package demo;  
  2.   
  3. import org.apache.commons.codec.binary.Base64;  
  4.   
  5. public class Base64Test {  
  6.   
  7.     public static void main(String[] args) {  
  8.   
  9.         Base64 base64 = new Base64();  
  10.         String str = "中文";  
  11.         byte[] enbytes = null;  
  12.         String encodeStr = null;  
  13.         byte[] debytes = null;  
  14.         String decodeStr = null;  
  15.   
  16.         enbytes = base64.encode(str.getBytes());  
  17.         encodeStr = new String(enbytes);  
  18.         debytes = base64.decode(enbytes);  
  19.         decodeStr = new String(debytes);  
  20.       
  21.         System.out.println("编码前:" + str);  
  22.         System.out.println("编码后:" + encodeStr);  
  23.         System.out.println("解码后:" + decodeStr);  
  24.     }  
  25. }  

B.Hex 编解码

 

 

  1. package demo;  
  2.   
  3. import org.apache.commons.codec.DecoderException;  
  4. import org.apache.commons.codec.binary.Hex;  
  5.   
  6. public class HexTest {  
  7.   
  8.     public static void main(String[] args) throws DecoderException {  
  9.   
  10.         Hex hex = new Hex();  
  11.         String str = "中文";  
  12.         char[] enbytes = null;  
  13.         String encodeStr = null;  
  14.         byte[] debytes = null;  
  15.         String decodeStr = null;  
  16.   
  17.         enbytes = hex.encodeHex(str.getBytes());  
  18.         encodeStr = new String(enbytes);  
  19.         debytes = hex.decodeHex(enbytes);  
  20.         decodeStr = new String(debytes);  
  21.   
  22.         System.out.println("编码前:" + str);  
  23.         System.out.println("编码后:" + encodeStr);  
  24.         System.out.println("解码后:" + decodeStr);  
  25.     }  
  26. }  

C.Metaphone 及 Soundex 编码

 

  1. package demo;  
  2.   
  3. import org.apache.commons.codec.language.Metaphone;  
  4. import org.apache.commons.codec.language.RefinedSoundex;  
  5. import org.apache.commons.codec.language.Soundex;  
  6.   
  7. public class LanguageTest {  
  8.   
  9.     public static void main(String[] args) {  
  10.   
  11.         Metaphone metaphone = new Metaphone();  
  12.         RefinedSoundex refinedSoundex = new RefinedSoundex();  
  13.         Soundex soundex = new Soundex();  
  14.         for (int i = 0; i < 2; i++) {  
  15.             String str = (i == 0) ? "resume" : "resin";  
  16.             String mString = null;  
  17.             String rString = null;  
  18.             String sString = null;  
  19.             try {  
  20.                 mString = metaphone.encode(str);  
  21.                 rString = refinedSoundex.encode(str);  
  22.                 sString = soundex.encode(str);  
  23.             } catch (Exception ex) {  
  24.                 ;  
  25.             }  
  26.             System.out.println("Original:" + str);  
  27.             System.out.println("Metaphone:" + mString);  
  28.             System.out.println("RefinedSoundex:" + rString);  
  29.             System.out.println("Soundex:" + sString + "/n");  
  30.         }  
  31.     }  
  32. }  

Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发.

更多关于Commons codec的详细信息,或者下载地址请点这里

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值