How to implement sdbm hash function in C #?

How can a hash function sdbm(like this ) be implemented in C #?

+3
source share
3 answers

You can take the C code with almost no change:

uint sdbm( string str )
{
    uint hash = 0;
    foreach( char ch in str )
    {
        hash = ch + (hash << 6) + (hash << 16) - hash;
    }
    return hash;
}

Or did you think of something more complex?

+4
source

I don't have a C compiler, so I can’t check if it works the same way, but I think the following is true:

private static ulong SBDM(string str)
{
    ulong hash = 0;

    foreach (char c in str)
    {
        hash = c + (hash << 6) + (hash << 16) - hash;
    }

    return hash;
}

If you just need to get the hash of the string and it doesn't really matter what the implementation is, you can always do the command String.GetHashCode ();

+1
source

++ #. , str .

private uint sdbm(byte[] str)
{
    uint hash = 0;

    foreach (char ch in str)
        hash = ch + (hash << 6) + (hash << 16) - hash;

    return hash;
}

, , BitConverter.GetBytes.

uint Hash = sdbm(BitConverter.GetBytes(myID));
0

Source: https://habr.com/ru/post/1696446/


All Articles