scala/java/c++使用murmur3计算hash值保持跨语言之间一致

目标:在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
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值