BKDRHash php兑现

WBOY
Release: 2016-06-13 13:04:26
Original
1049 people have browsed it

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

<?php

function 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');//1471979560
echo BKDRHash('asdfasdfasdf123'); // 1220655578

?>
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template