import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* HEX字符串与字节码(字符串)转换工具
*/
public class HexUtils {
public static void main(String[] args) throws Exception {
test("a");//编码前【a】编码后【61】解码后【a】
test("A");//编码前【A】编码后【41】解码后【A】
test("1");//编码前【1】编码后【31】解码后【1】
test("白");//编码前【白】编码后【E799BD】解码后【白】
test("白乾涛");//编码前【白乾涛】编码后【E799BDE4B9BEE6B69B】解码后【白乾涛】
System.out.println(URLEncoder.encode("白乾涛", "UTF-8"));//和上述编码结果一致【%E7%99%BD%E4%B9%BE%E6%B6%9B】
System.out.println(encodeHex("白乾涛".getBytes("UTF-8")));//使用apache的工具类编码和上述编码结果一致【e799bde4b9bee6b69b】
System.out.println(new String(decodeHex("e799bde4b9bee6b69b".toCharArray()), "UTF-8"));
}
private static void test(String input) throws UnsupportedEncodingException {
String charsetName = "UTF-8";
System.out.print("编码前【" + input + "】");
System.out.print("编码后【" + str2HexStr(input, charsetName) + "】");
System.out.println("解码后【" + hexStr2Str(str2HexStr2(input, charsetName), charsetName) + "】");
}
//******************************************************************************************
// 参数或返回值为字符串
//******************************************************************************************
/**