我正在尝试将以下scala代码移植到c ++
标量代码:
def getLong(bytes: Array[Byte]): Long = {
//MessageDigest.getInstance("MD5")
val hash = messageDigest.get().digest(bytes)
val str = messageDigest.get().digest(bytes).map(_.toChar).mkString("")
val bb = ByteBuffer.wrap(hash)
bb.order(ByteOrder.LITTLE_ENDIAN)
val number: Long = bb.getInt & 0xffffffffL
number
}
C ++代码:
unsigned long get_long(string key) {
unsigned long n = key.length();
unsigned char* uchrs = reinterpret_cast<unsigned char*>(const_cast<char*>(key.c_str()));
unsigned char outbuf[MD5_DIGEST_LENGTH];
MD5(uchrs, n, outbuf);
unsigned long number = *((unsigned long*)outbuf);
return number;
}
当我用此输入调用函数时,它给出不同的输出值,有人知道我在做什么错吗?
示例:
getLong("158750493953113325943f6e61350d949848bd2877f97aee4b6".getBytes) => 2384178603
get_long("158750493953113325943f6e61350d949848bd2877f97aee4b6") => 15019855082967248299
我在C ++代码中尝试过类似的事情:
int num_int = number & INT_MAX & 0xffffffff;
这无济于事(返回不同的值236694955)。