in reply to Generating A Hash Value
When you say "using int arithmetic"; signed or unsigned? 32-bit?
Could you supply the hash value generated for "The quick brown fox jumps over the lazy dog" so that we may have something to check our attempts against.
This might be close, and reasonable efficient as it avoids the exponentiation by processing the string backwards and accumulating the exponent, and uses integer math. However truncating to 32-bits in reverse order maybe screwing up the result?
#! perl -slw use strict; # s[0]*31^(n-1)+s[1]*31^(n-2)+ ... +s[n-1] sub JavaHash { use integer; my ($s) = @_; my $n = length $s; my @s = unpack 'C*', $s; my $hash = 0; my $p = 1; for ( reverse @s ) { print "$hash : $_: $p"; $hash += $_ * $p; $p *= 31; } return $hash; } die 'No arguments' unless @ARGV; printf '%10d:%s' , JavaHash( $_ ), $_ for @ARGV;
|
|---|