1
如何將給定String的UTF-8編碼字節的MD5散列的前8個字節表示爲java中的long?如何在Java中將長度爲給定字符串的UTF-8編碼的字節的MD5散列的前8個字節表示爲長整型?
如何將給定String的UTF-8編碼字節的MD5散列的前8個字節表示爲java中的long?如何在Java中將長度爲給定字符串的UTF-8編碼的字節的MD5散列的前8個字節表示爲長整型?
public static void main(final String[] args) throws Exception
{
final MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[] digest = md5.digest("Grommit".getBytes("UTF-8"));
long result = 0;
for (int i = 0; i < 8; i++)
{
System.out.println(Long.toHexString(0xFFL & digest[i]));
result |= (0xFFL & digest[i]) << (i * 8);
}
System.out.println(Long.toHexString(result));
}
試試這個:
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest("Wallace".getBytes("UTF-8"));
long result = ByteBuffer.wrap(digest).getLong();
Booya!關於API的知識勝過了點點滴滴的知識。 +1 –