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 Link to comment https://forums.phpfreaks.com/topic/202069-help-generate-hash-code/ 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"); Link to comment https://forums.phpfreaks.com/topic/202069-help-generate-hash-code/#findComment-1059662 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. Link to comment https://forums.phpfreaks.com/topic/202069-help-generate-hash-code/#findComment-1059813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.