我希望将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);
}
需要进行哈希的字符串类似于"SAEUI1210.00000010_1"。
然而,在某些情况下,这会产生重复值。 是否有任何好的替代方案,可以避免为不同的字符串值生成相同的哈希值。