Commons codec provides Base64, Hex, and Metaphone, Soundex and other coding algorithms.
Download Address: http://commons.apache.org/codec/
A.BASE64 Coding and decoding
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 Coding and decoding
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);
}
}