*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashCode to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashCode to String" Watch "HashCode to String" New topic
Author

HashCode to String

manishsharma sharma
Greenhorn

Joined: Jun 17, 2005
Posts: 24
Hi guys ,

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

Thanks


Thanks, Manish
SCJP-100, SCMAD-91, SCWCD-89, http://javatechtips.blogspot.com/
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41587
    
  54
No, that's not possible. Hashing is a one-way transformation - once something is hashed, there is no way of recovering the original information.


Ping & DNS - my free Android networking tools app
manishsharma sharma
Greenhorn

Joined: Jun 17, 2005
Posts: 24
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

Joined: Jun 01, 2006
Posts: 52
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jun 01, 2006
Posts: 52
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jun 01, 2006
Posts: 52
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

Joined: Jan 30, 2000
Posts: 18671
Yep, absolutely.

Except for Barry's misuse of the term "unique".
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
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.
 
 
subject: HashCode to String