Skip to content

Instantly share code, notes, and snippets.

@dongqs
Created September 10, 2014 12:30
Show Gist options
  • Save dongqs/2a42a2aa8355ae93933d to your computer and use it in GitHub Desktop.
Save dongqs/2a42a2aa8355ae93933d to your computer and use it in GitHub Desktop.
java string hashCode() ruby implementation
TWO_31 = 2 ** 31
TWO_32 = 2 ** 32
def java_hash_code(str)
size = str.size
hash = 0
str.chars.each_with_index do |ch, i|
hash += ch.ord * (31 ** (size-(i+1)))
hash = hash % TWO_32 - TWO_31
end
hash
end
@bradurani
Copy link

This is not correct. Try, just converting , for instance. It should return 44, but it doesn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment