传递"字符串"将对象转换为DJB2哈希函数

时间:2015-10-25 01:28:31

标签: c++ string hash

我使用的是djb2哈希函数的标准char *版本:

RewriteEngine On
RewriteRule ^(application) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

如何编辑此哈希函数以接受String对象而不是unsigned char *?

1 个答案:

答案 0 :(得分:0)

请改用字符串?

unsigned long DJB2(std::string str){
    unsigned long hash = 5381;
    int c;

    for (int i = 0; i < str.length(); ++i) {
        c = (int) str[i];
        hash = ((hash << 5) + hash) + c; 
    }
    return hash;
}