我正在尝试散列一个unsigned long
值,但散列函数需要一个unsigned char *
,如下面的实现所示:
unsigned long djb2(unsigned char *key, int n)
{
unsigned long hash = 5381;
int i = 0;
while (i < n-8) {
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
}
while (i < n)
hash = hash * 33 + key[i++];
return hash;
}
有没有办法可以实现我的目标,也许是两者之间的演员?