@Test
public void testHexAndBase64(){
String data = "你在哪儿,我的天你在哪儿" ;
byte[] bytes = data.getBytes() ;
try {
long start = System.currentTimeMillis() ;
String encryStr = "" ;
String decryStr = "" ;
for(int i= 0 ; i < 10000 ; i++) {
//	 encryStr = CryptoUtil.getInstance().byte2HexStr(bytes) ;
encryStr = Hex.encodeHexString(bytes) ;
decryStr = new String(Hex.decodeHex(encryStr.toCharArray())) ;
//	 decryStr = new String(CryptoUtil.getInstance().hexStr2Byte(encryStr)) ;
}
print("Hex:"  + (System.currentTimeMillis() - start ) + " ms " +  encryStr  + " | " + decryStr) ;

} catch (Exception e) {
e.printStackTrace();
}

try {
long start = System.currentTimeMillis() ;
String encryStr = "" ;
String decryStr = "" ;
for(int i= 0 ; i < 10000 ; i++) {
encryStr = Base64.encodeBase64String(bytes) ;
decryStr = new String(Base64.decodeBase64(encryStr.getBytes()) );
}
print("base64: " + (System.currentTimeMillis() - start ) + " ms " +  encryStr  + " | " + decryStr) ;

} catch (Exception e) {
e.printStackTrace();
}
}

结果:

Hex:31 ms e4bda0e59ca8e593aae584bfefbc8ce68891e79a84e5a4a9e4bda0e59ca8e593aae584bf | 你在哪儿,我的天你在哪儿
base64: 80 ms 5L2g5Zyo5ZOq5YS/77yM5oiR55qE5aSp5L2g5Zyo5ZOq5YS/ | 你在哪儿,我的天你在哪儿

明显这是速度与空间选择不同,16进制编码速度明显快,但体积要大,base64相对慢些,编码后相对较小,根据实际情况选择

05-04 13:44