@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(); } }
結果: java
Hex:31 ms e4bda0e59ca8e593aae584bfefbc8ce68891e79a84e5a4a9e4bda0e59ca8e593aae584bf | 你在哪兒,個人天你在哪兒
base64: 80 ms 5L2g5Zyo5ZOq5YS/77yM5oiR55qE5aSp5L2g5Zyo5ZOq5YS/ | 你在哪兒,個人天你在哪兒
編碼
明顯這是速度與空間選擇不一樣,16進制編碼速度明顯快,但體積要大,base64相對慢些,編碼後相對較小,根據實際狀況選擇 code