Home  >  Article  >  Backend Development  >  BKDRHash php兑现

BKDRHash php兑现

WBOY
WBOYOriginal
2016-06-13 11:02:411006browse

BKDRHash php实现
接上一帖 BKDRHash的php实现 比c语言版本复杂的部分,是由于php中整型数的范围是,且一定是-2147483648 到2147483647,并且没有无符号整形数,在算法中会出现大数溢出的问题,不能使用intval,需要用floatval,同时在运算过程中取余保证不溢出。

<?phpfunction BKDRHash($str){	$seed = 131; // 31 131 1313 13131 131313 etc..	$hash = 0;		$cnt = strlen($str);	for($i = 0; $i < $cnt; $i++)	{				$hash = ((floatval($hash * $seed) & 0x7FFFFFFF) + ord($str[$i])) & 0x7FFFFFFF;			}	return ($hash & 0x7FFFFFFF);}echo BKDRHash('ggsonic');//1471979560echo BKDRHash('asdfasdfasdf123'); // 1220655578?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP开发工具集一Next article:PHP文件上载过滤类