Python函數(shù)是以下的,python中可以指定字節(jié)的排列順序,比如 'little',java中默認是大字節(jié)序屎慢,通過使用ArrayUtils來反轉字節(jié)數(shù)組來達到使用小字節(jié)序的目的码倦,字節(jié)數(shù)組轉long的過程中會轉化成有符號的long類型,解決辦法是使用BigInteger類來實現(xiàn)unsigned,具體代碼如下:
要用java方式實現(xiàn)的python的代碼
import hashlib
md5 = hashlib.md5("this is str".encode("utf-8"))
low = int.from_bytes(md5.digest()[0:8],'little')
java代碼實現(xiàn)
//導入需要的包
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
import java.math.BigInteger;
public static String byteArrayToLong(byte[] array) {
byte[] lowArray = Arrays.copyOfRange(array,0,8);
byte[] highArray = Arrays.copyOfRange(array,8,16);? //分別拷貝字節(jié)數(shù)組的前八位和后八位
ArrayUtils.reverse(lowArray);//java中默認的字節(jié)排列順序為big_endian,用工具類將字節(jié)數(shù)組翻轉改成little_endian
ArrayUtils.reverse(highArray);
BigInteger lowBigInt =new BigInteger(1,lowArray);
BigInteger highBigInt =new BigInteger(1,highArray);
return lowBigInt.toString();//返回的就是無符號的long類型的字符串
}