找到一个hash函数

前言

在lua5.3.3代码中看到一个hash函数,摘出来玩玩。

试验

// hw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

unsigned int hashProc(const char* pcSrc, size_t l, unsigned int seed)
{
    unsigned int h = seed ^(unsigned int)l;
    size_t step = (l >> 5) + 1;

    for (; l >= step; l -= step) {
        h ^= ((h << 5) + (h >> 2) + (BYTE)pcSrc[l - 1]);
    }

    return h;
}

int main(int argc, char* argv[])
{
    srand((UINT)time(NULL));
    const char* pcMsg = "string for calc hash";
    UINT iHash = hashProc(pcMsg, strlen(pcMsg), rand());
    printf("hash value = 0x%X, str = [%s]\r\n", iHash, pcMsg);
    system("pause");

    /** run result
    hash value = 0xEE996D0, str = [string for calc hash]
    请按任意键继续. . .
    */
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值