如何散列code对象值转换为字符串变量 [英] How to convert a hash code object value into a string variable
问题描述
我想转换我在logacat文件收到一个String重新presentation哈希code值。
我曾尝试下面的code。
最后一个元素E =(元)nodes_array.item(指数);
最后的节点列表nodeKey = e.getElementsByTagName(钥匙);
的System.out.println($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+ nodeKey.item(1)的ToString( ));
和得到了下面的输出在logcat的文件 -
I /的System.out(919):$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$org.apache.harmony.xml.dom.ElementImpl@b2dd54a0
没有,这是不可能的。散列是单向变换 - 一旦东西被散列,没有恢复原始的信息的方式
散列code的概念是,使得它有可能为多个对象具有相同的哈希code。
例如:的
第一串=ABCDEa123abc;
第二个字符串=ABCDFB123abc;
块引用>公式计算字符串的哈希值code是这样的:
S0器X31 ^(N-1)+ S1器X31 ^(N-2)+ ... + S(N-2)×31 + S(N- 1)
其中,
取值
表示字符串中的字符,N
是字符串的长度。这个公式从来没有保证了唯一的哈希code到每个字符串。
ABCDEa123abc
和ABCDFB123abc
具有相同的哈希code,但不是因为相同的字符序列是不同的。这将导致以下 -
INT HASH1 =ABCDEa123abc.hash code();
INT HASH2 =ABCDFB123abc.hash code();注意: HASH1和HASH2相等的,但2字符串是不平等的。
所以哈希code不能唯一地标识字符串。
由于记录,
有关
等于()
和散code()
,它是写,如果等于()
两个对象返回true,说'A'
和'B'
,那么他们的
散列codeS必须相同。但是,它进一步说,哈希codeS需要
没有不同,如果等于()
返回false。
块引用>更新:
就像你做得到对象的哈希code
Object.toString()
,就可以得到字符串变量的哈希值code这样做 -的System.out.println(Integer.toHexString(s.hash code()));
如需了解更多有关哈希code检 -
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, andn
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 -
the-3-things-you-should-know-about-hashcode.
这篇关于如何散列code对象值转换为字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!