Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器 软件资源

软件开发小程序制作系统集成与运维空间租用硬件开发视频监控技术咨询与支持——联系电话:0311-88999002/88999003

首页 / 软件开发 / 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);
}
}