//將字節(jié)數(shù)組轉(zhuǎn)換為16進(jìn)制字符串
public static String BinaryToHexString(byte[] bytes) {
String hexStr = "0123456789ABCDEF";
String result = "";
String hex = "";
for (byte b : bytes) {
hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
hex += String.valueOf(hexStr.charAt(b & 0x0F));
result += hex + "";
}
return result;
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
// toUpperCase將字符串中的所有字符轉(zhuǎn)換為大寫(xiě)
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
// toCharArray將此字符串轉(zhuǎn)換為一個(gè)新的字符數(shù)組唉铜。
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
//charToByte返回在指定字符的第一個(gè)發(fā)生的字符串中的索引睡雇,即返回匹配字符
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static byte[] beforeAnalyze(byte[] beforCode){
//轉(zhuǎn)成字符串
String s = new String(beforCode);
//再拆分成對(duì)應(yīng)的16進(jìn)制
return hexStringToBytes(s);
}
都是一些比較實(shí)用的方法沥阳,保存一下