从C#可变长度字符串固定长度的数字散列code [英] Fixed Length numeric hash code from variable length string in c#

查看:297
本文介绍了从C#可变长度字符串固定长度的数字散列code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储固定长度(最多8位)的一个可变长度的字符串产生的数字。散列不必是唯一的。它只是需要在输入字符串的变化来改变。有没有在.net中的散列函数这样做吗?

I need to store fixed-length (up to 8 digits) numbers produced from a variable length strings. The hash need not be unique. It just needs to change when input string changes. Is there a hash function in .Net that does this?

感谢
纪。

Thanks
Kishore.

推荐答案

我认为你这样做是因为你需要保存在其他位置值和比较反对。因此扎克的回答(虽然完全正确的),可能会导致你的问题,因为合同String.GetHash code()是明确的关于它的范围发生变化。

I assume you are doing this because you need to store the value elsewhere and compare against it. Thus Zach's answer (while entirely correct) may cause you issues since the contract for String.GetHashCode() is explicit about its scope for changing.

因此​​,这里是一个固定的,容易重复在其他语言版本。

Thus here is a fixed and easily repeatable in other languages version.

我想你也知道在编译时的小数位数可用。 这是基于对詹金斯一次一个哈希(如实施和严苛的测试,通过布雷特穆维),因此它具有优良的雪崩行为(在输入一位传播到输出的所有位的变化),这意味着在位有点懒模数减少的到底是不是一个严重的缺陷在大多数应用中(虽然你可以用更复杂的行为做的更好)

I assume you will know at compile time the number of decimal digits available. This is based on the Jenkins One At a Time Hash (as implemented and exhaustively tested by Bret Mulvey), as such it has excellent avalanching behaviour (a change of one bit in the input propagates to all bits of the output) which means the somewhat lazy modulo reduction in bits at the end is not a serious flaw for most uses (though you could do better with more complex behaviour)

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)
}

这篇关于从C#可变长度字符串固定长度的数字散列code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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