如何在 C 中编写 djb2 哈希函数?

How to write djb2 hashing function in C?

我不知道如何在 C 上编写 djb3 哈希函数。

我在网上搜索了一下,但我发现它可能是用 C++ 编写的...

unsigned long hash(unsigned char *str){
        unsigned long hash = 5381;
        int c;

        while (c = *str++)
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

        return hash;
    }

如果你能向我解释一下,我将非常感激...

这里是djb2哈希函数的C语言代码!

unsigned int hash(const char *word)
{
    unsigned int hash = 5381;
    int c;

    while ((c = *word++))        // *str++ is going to the next address in memory, where the next char in the string is stored
    {
        if (isupper(c))
        {
            c = c + 32;
        }

        hash = ((hash << 5) + hash) + c; // hash * 33 + c   // hash << 5 = hash * 2^5
    }

    return hash % N;
}

感谢@500-InternalServerError 解决了我的疑惑