• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashCode to String

 
Anthony Alberts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the String method hashCode() to obtain the hash value of a specified String. I need to to convert the hash value back to the original String. Can anybody help? Essentialy this should be simple but I am stumped!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Alberts:
I am using the String method hashCode() to obtain the hash value of a specified String. I need to to convert the hash value back to the original String. Can anybody help? Essentialy this should be simple but I am stumped!

It's not only not simple, it's impossible!
First, there is no guarantee that two different Strings also differ in their hashcode. In fact there *have* to be Strings which are not equal but share the same hashcode - after all, there are much more possible values for a String than possible values for an int!
Second, there is no guarantee on how the hashcode of a String is calculated, afaik - the algorithm might very well differ strongly between JRE implementations.
 
Anthony Alberts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. I assume then that my understanding of Hash leaves much to be desired or possibly smo'kn too much of the above.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...then you may appreciate taking a look at Roedy Green's Java Glossary on hashCode.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic