package com.guigu.zl.util;
public class ByteArrayTransformHexString {
private static final char[] CHARHEX = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
private static final String HEXSTRING = "0123456789abcdef";
public static String byteArrayToHexString(byte[] arr) {
char [] c = new char[arr.length * 2];
int index = 0;
for ( byte a : arr) {
c[index++] = CHARHEX[a & 0xf];
c[index++] = CHARHEX[a>>>4 & 0xf];
}
return new String(c);
}
public static byte[] hexStringToByteArray(String str) {
byte [] arr = new byte[str.length()/2];
char [] crr = str.toCharArray();
int index = 0;
for (int i = 0; i < crr.length ; ) {
int low = HEXSTRING.indexOf(crr[i++]);
int hight = HEXSTRING.indexOf(crr[i++]);
arr[index++] = (byte)(hight << 4 | low);
}
return arr;
}
}