目标:在java/scala中对word计算出无符号数字(uint64)的hash值,与c++中计算的hash值一样
方案:
因为java没有uint64,而c++的murmur3是用的uint64,所以java这边直接asLong会出现负数,所以需要取原始二进制值,转换成guava提供的无符号long
scala端:
import com.google.common.hash.Hashing
import com.google.common.primitives.UnsignedLong
...
private def hash(word: String): String = {
UnsignedLong.valueOf(Hashing.murmur3_128().hashString(word,StandardCharsets.UTF_8).asLong().toBinaryString,2).toString
}
c++端:
uint64_t hash(const std::string& word) {
int str_len = word.length();
const char* c_origin = word.c_str();
uint64_t res[2];
const uint32_t seed = 0;
MurmurHash3_x64_128(c_origin, str_len, seed, res);
return res[0];
}
...
void MurmurHash3_x64_128 ( const void * key, const int len,
const uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int n

最低0.47元/天 解锁文章
6278





