I am trying to convert a hash code value which I received in my logacat file to a String representation.
I have tried the below code .
final Element e = (Element)nodes_array.item(index);
final NodeList nodeKey = e.getElementsByTagName("key");
System.out.println(" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+nodeKey.item(1).toString());
and got the below output in the logcat file-
I/System.out(919): $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$org.apache.harmony.xml.dom.ElementImpl@b2dd54a0
No, that's not possible. Hashing is a one-way transformation - once something is hashed, there is no way of recovering the original information.
The concept of hash code is such that it is possible for multiple object to have the same hash code.
For example:
String first = "ABCDEa123abc";
String second = "ABCDFB123abc";
Formula for calculating the hash code of a string is something like:
S0 X 31 ^ (n-1) + S1 X 31 ^ (n-2) + .... + S(n-2) X 31 + S(n-1)
Where S
indicates the character in the string, and n
is the length of the string.
This formula never guarantees an unique hash code to each string.
"ABCDEa123abc"
and "ABCDFB123abc"
has the same hash code but are not equal since their sequence of characters are different.
This will result in the following-
int hash1 ="ABCDEa123abc".hashCode();
int hash2 = "ABCDFB123abc".hashCode();
NOTE: hash1 and hash2 are equal but the 2 strings are unequal.
So hashcode cannot uniquely identify the string.
As documented,
For
equals()
andhashCode()
, it is written that ifequals()
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, ifequals()
returns false.
UPDATE:
Just like you get the hashcode of the object by doing Object.toString()
, you can get the hashcode of the String variable by doing -
System.out.println(Integer.toHexString(s.hashCode()));
To learn more about Hashcode check -