不要将摘要转换为数字!
使用类似的东西:
byte[] b = md5Checksum.digest();
// now convert these bytes to chars
将 byte[] 转换为 HexStrings 有许多不同的方法:
public class HexConverter {
// thanks to http://www.rgagnon.com/javadetails/java-0596.html
static private final String HEXES = "0123456789ABCDEF";
public String toHex(byte[] raw) {
if (raw == null) {
return null;
}
final StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
}
或从在 Java 中获取文件的 MD5 校验和
(此链接还显示了如何从 Apache Commons Codec 获取 DigestUtils)
public static String getMD5Checksum(String filename) throws Exception {
byte[] b = createChecksum(filename);
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}