public class HexUtil {
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
char[] hexArray = "0123456789ABCDEF".toCharArray();
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexToByteArray(String hexString) {
int hexlen = hexString.length();
byte[] result;
if (hexlen % 2 == 1){
//奇数
hexlen++;
result = new byte[(hexlen/2)];
hexString="0"+hexString;
}else {
//偶数
result = new byte[(hexlen/2)];
}
int j=0;
for (int i = 0; i < hexlen; i+=2){
result[j]= (byte) Integer.parseInt(hexString.substring(i,i+2),16);
j++;
}
return result;
}
|