我需要存储从可变长度字符串生成的固定长度(最多8位)数字.哈希不必是唯一的.它只需要在输入字符串更改时更改..Net中是否有哈希函数来执行此操作?
由于
纪.
Shu*_*oUk 23
我假设你这样做是因为你需要将值存储在别处并与之进行比较.因此,Zach的答案(虽然完全正确)可能会引起您的问题,因为String.GetHashCode()的合同明确了它的更改范围.
因此,这是一个固定的,易于重复的其他语言版本.
我假设您将在编译时知道可用的小数位数.这是基于Jenkins One At a Time Hash(由Bret Mulvey 实施和详尽测试),因此它具有出色的雪崩行为(输入中的一位变化传播到输出的所有位),这意味着对于大多数用途而言,懒惰的模数减少并不是一个严重的缺陷(尽管你可以用更复杂的行为做得更好)
const int MUST_BE_LESS_THAN = 100000000; // 8 decimal digits
public int GetStableHash(string s)
{
uint hash = 0;
// if you care this can be done much faster with unsafe
// using fixed char* reinterpreted as a byte*
foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
{
hash += b;
hash += (hash << 10);
hash ^= (hash >> 6);
}
// final avalanche
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
// helpfully we only want positive integer < MUST_BE_LESS_THAN
// so simple truncate cast is ok if not perfect
return (int)(hash % MUST_BE_LESS_THAN);
}
Run Code Online (Sandbox Code Playgroud)
简单方法(请注意,这取决于平台):
int shorthash = "test".GetHashCode() % 100000000; // 8 zeros
if (shorthash < 0) shorthash *= -1;
Run Code Online (Sandbox Code Playgroud)