比如字节数组转十六进制:
public static String getMd5(String content) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(content.getBytes());
//消息摘要的结果一般都是转换成16进制字符串形式展示
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i]&0xff);
if (hex.length() == 1){
sb.append("0");
}
sb.append(hex);
}
String hexstring = sb.toString();
Log.d("getMd5() hexstring= : " , "hexstring=" + hexstring );
//例如 hexstring=ce4c373d4c2ecc75edb733cd8c67d360
return hexstring;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
一个字节是8位,一个十六进制是4位,则8位需要2个十六进制,因此Integer.toHexString(bytes[i]&0xff)
即可将一个字节的数据转为十六进制显示。