从boost ::哈希获得32位的哈希值 [英] Get 32-bit hash value from boost::hash

查看:178
本文介绍了从boost ::哈希获得32位的哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的boost ::哈希来得到一个字符串的哈希值。
但它在Windows 32位和Debian 64位系统提供了相同的字符串不同的哈希值。

I am using boost::hash to get hash value for a string. But it is giving different hash values for same string on Windows 32-bit and Debian 64-bit systems.

所以,我怎么能得到使用相同的散列值(32位或64位)的boost ::哈希不论平台?

So how can I get same hash value (32-bit or 64-bit) using boost::hash irrespective of platform?

推荐答案

什么是关于的boost ::哈希的保证吗?我没有看到任何
保证生成的哈希值code是可用外
过程中产生的。 (这是经常与壳体
Hash函数)。如果你需要外部数据的哈希值,
有效过不同的程序和不同的平台(例如,对于
哈希访问磁盘上的数据),那么你就必须写你
拥有。是这样的:

What is the guarantee concerning boost::hash? I don't see any guarantees that a generated hash code is usable outside of the process which generates it. (This is frequently the case with hash functions.) If you need a hash value for external data, valid over different programs and different platforms (e.g. for a hashed access to data on disk), then you'll have to write your own. Something like:

uint32_t
hash( std::string const& key )
{
    uint32_t results = 12345;
    for ( auto current = key.begin(); current != key.end(); ++ current ) {
        results = 127 * results + static_cast<unsigned char>( *current );
    }
    return results;

}

应该做的伎俩,只要你不担心
移植到一些异国情调的大型机(这可能不支持
uint32_t的)。

这篇关于从boost ::哈希获得32位的哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆