Welcome 微信登录

首页 / 软件开发 / JAVA / Jakarta-Common-Codec使用笔记

Jakarta-Common-Codec使用笔记2011-01-28 csdn博客 沈斌commons codec 提供 base64, hex, 及 metaphone, soundex 等编码演算。

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

A.Base64 编解码

package demo;

import org.apache.commons.codec.binary.Base64;

public class Base64Test ...{

public static void main(String[] args) ...{

Base64 base64 = new Base64();
String str = "中文";
byte[] enbytes = null;
String encodeStr = null;
byte[] debytes = null;
String decodeStr = null;

enbytes = base64.encode(str.getBytes());
encodeStr = new String(enbytes);
debytes = base64.decode(enbytes);
decodeStr = new String(debytes);

System.out.println("编码前:" + str);
System.out.println("编码后:" + encodeStr);
System.out.println("解码后:" + decodeStr);
}
}

B.Hex 编解码

package demo;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class HexTest ...{

public static void main(String[] args) throws DecoderException ...{

Hex hex = new Hex();
String str = "中文";
char[] enbytes = null;
String encodeStr = null;
byte[] debytes = null;
String decodeStr = null;

enbytes = hex.encodeHex(str.getBytes());
encodeStr = new String(enbytes);
debytes = hex.decodeHex(enbytes);
decodeStr = new String(debytes);

System.out.println("编码前:" + str);
System.out.println("编码后:" + encodeStr);
System.out.println("解码后:" + decodeStr);
}
}