public static String bytesToHexString(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString().toUpperCase();
}
参考链接:
另外一种方式(效率非常非常低,仅为了展示格式化输出,不推荐使用)
public static String bytesToHexString(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
stringBuffer.append(String.format("%02x", bytes[i]));
}
return stringBuffer.toString();
}