我想将一个char数组哈希到一个int或long.结果值必须符合给定的精度值.我一直在使用的功能如下:
int GetHash(const char* zKey, int iPrecision /*= 6*/)
{
/////FROM : http://courses.cs.vt.edu/~cs2604/spring02/Projects/4/elfhash.cpp
unsigned long h = 0;
long M = pow(10, iPrecision);
while(*zKey)
{
h = (h << 4) + *zKey++;
unsigned long g = h & 0xF0000000L;
if (g) h ^= g >> 24;
h &= ~g;
}
return (int) (h % M);
}
Run Code Online (Sandbox Code Playgroud)
要哈希的字符串类似于"SAEUI1210.00000010_1".
但是,在某些情况下,这会产生重复值.是否有任何好的替代方案不会为不同的字符串值复制相同的哈希值.
归档时间: |
|
查看次数: |
11867 次 |
最近记录: |