aisatriani Posted May 17, 2010 Share Posted May 17, 2010 please help .. I have a function the following hashgenerate <?php function HashCode($val) { $maxInt = 4294967295; $maxPostInt = 2147483647; $h = 0; $value = String_to_ASCII($val); for($i=0;$i<count($value);$i++) { $h = $h*31 + $value[$i]; if($h > $maxInt){ $div = intval($h/($maxInt + 1)); $h = $h - ($div * ($maxInt + 1)); } if($h > $maxPostInt){ $h = $h - $maxInt - $i; } } return $h; } function String_to_ASCII($string) { $ascii = NULL; $value = array(); for($i=0;$i < strlen($string);$i++) { array_push($value,ord($string[$i])); } return $value; } } echo HashCode("JYeHn8Tu5donkey")); ?> from the above function to generate strings will produce JYeHn8Tu5donkey = -5.52888247115E+022 hello = 99162322 and i want to generate these JYeHn8Tu5donkey to generate numbers -22635908 function what is wrong? please guidance Quote Link to comment Share on other sites More sharing options...
scampbell Posted May 17, 2010 Share Posted May 17, 2010 For a start } echo HashCode("JYeHn8Tu5donkey")); should be echo HashCode("JYeHn8Tu5donkey"); Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted May 17, 2010 Share Posted May 17, 2010 $div = intval($h/($maxInt + 1)); $h = $h - ($div * ($maxInt + 1)); is wrong, somehow wrong. You are in essence setting $h=$h-$h. It just comes to a number close to zero. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.