Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashCode to String

 
manishsharma sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys ,

I have a hashcode value "1997559548" , can any one hava a method which can convert it back to String.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not possible. Hashing is a one-way transformation - once something is hashed, there is no way of recovering the original information.
 
manishsharma sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I found that for each string there exists a unique hashcode which is calculated by their content( I mean character they contain , by a formula)

correct me if I wrong
Thanks
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I do not know the exact way String class implements hashCode, the concept of hash code is such that it is possible for multiple object to have the same hash code.
Say if I have an approach to add the position of each character to evaluate the hash code the consider the following
String HashCode
------ --------
ABC 1+2+3=6 [position of A is 1, B is 2 and so on]
= 6 is the hash code

CAB 3+1+2=6

BD 2+4=6

Now given 6 (the hashcode) it is not possible to determine uniquely the whether the object was ABC, BD or CAB.
Note that this may not be the way String calculates hash code. But even with a different approach or formula, a similar situation can arise which will prevent you from getting the exact String. So just the hash code is not enough to uniquely determine the String.
BUT IN CASE STRING HAS A FORMULA IN WHICH EVERY STRING OBJECT HAS A DIFFERENT HASHCODE VALUE[the best and ideal hash-code algorithm], THEN IT SHOULD BE POSSIBLE. BUT THAT IS MORE UNLIKELY.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because every string has a unique hashcode (a many-to-one mapping from strings to integers), it does not follow that every hashcode value has a unique string mapping to it.
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am now certain that you cannot find the String from its hashcode. String calculates hash code as :
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

This formula never guarantees an unique hash code to each string. For example:
"ABCDEa123abc" and "ABCDFB123abc" has the same hash code but are not equal since their sequence of characters are different. To iterate, the following will happen:-
1.
int hash1 ="ABCDEa123abc".hashCode();
int hash2 = "ABCDFB123abc".hashCode();
//hash1 and hash2 are equal
2.The 2 strings are unequal

So hashcode cannot uniquely identify the string.
As I had mentioned earlier, if you can come up with a hash code formula so that every object has a different hash code and hash code not repeating ever, then it can be considered as the ideal hash code.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit wrote:
This formula never guarantees an unique hash code to each string


So you are saying a string can have two (or more) hashcode values?
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Not at all.
I said that 2 strings can have the same hash-code. The example that followed illustrated it. The 2 strings I showed have the same hash-code but not the same sequence of characters[hence not equal. equals method returns false].
Please examine the same once more.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry, you seem to be using "unique" a bit differently than I would. I would say each string has one hashcode, but I would never call that "unique" since other strings can have the same hashcode. I think this is how most people would understand the term here. Or maybe it's just me and Amit.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what I mean is that hashcode() is a many-one mapping. For the same string you get a unique code (does not change when you execute hashcode() a second time during the lifetime of the JVM). Two strings can map onto that same unique hashcode. Going the other way the mapping is one-many, so given a hashcode you cannot find a unique string that maps to it. Or it could be, Jim, I am different since I fell off my bike a couple of weeks ago. In other words we are thinking about the same thing, but differently.
[ November 24, 2006: Message edited by: Barry Gaunt ]
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry wrote:

---------------------------------------------------------------------
In other words we are thinking about the same thing, but differently.

---------------------------------------------------------------------

We all are thinking the same thing in the same way. There is absolutely no difference in what we all are saying. There is nothing different in the understanding. There are no confusions to be debated or resolved.
Absolutely none.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, absolutely.

Except for Barry's misuse of the term "unique".
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the API documentation for equals() and hashCode(), it is written that if equals() returns true for two objects, say 'a' and 'b', then their hash-codes MUST be same. But, it further says that the hashcodes need not be different, if equals() returns false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic