java Java从哈希码重新创建字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6334572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:26:38  来源:igfitidea点击:

Java recreate string from hashcode

javastringhashcodeconstruct

提问by Richard J. Ross III

Is there any way that I can use a hashcode of a string in java, and recreate that string?

有什么方法可以在java中使用字符串的哈希码,然后重新创建该字符串?

e.g. something like this:

例如这样的事情:

String myNewstring = StringUtils.createFromHashCode("Hello World".hashCode());
if (!myNewstring.equals("Hello World"))
    System.out.println("Hmm, something went wrong: " + myNewstring);

I say this, because I must turn a string into an integer value, and reconstruct that string from that integer value.

我这样说是因为我必须将一个字符串转换为一个整数值,然后从该整数值重建该字符串。

采纳答案by Ted Hopp

This is impossible. The hash code for String is lossy; many String values will result in the same hash code. An integer has 32 bit positions and each position has two values. There's no way to map even just the 32-character strings (for instance) (each character having lots of possibilities) into 32 bits without collisions. They just won't fit.

这是不可能的。String 的哈希码是有损的;许多字符串值将产生相同的哈希码。一个整数有 32 位位置,每个位置有两个值。甚至只有 32 个字符的字符串(例如)(每个字符都有很多可能性)也无法映射到 32 位而不会发生冲突。他们只是不适合。

If you want to use arbitrary precision arithmetic (say, BigInteger), then you can just take each character as an integer and concatenate them all together. Voilà.

如果您想使用任意精度算术(例如 BigInteger),那么您可以将每个字符作为一个整数并将它们连接在一起。瞧。

回答by JustinKSU

No. Multiple Strings can have the same hash code. In theory you could create all the Strings that have have that hash code, but it would be near infinite.

不可以。多个字符串可以具有相同的哈希码。从理论上讲,您可以创建所有具有该哈希码的字符串,但它几乎是无限的。

回答by Andrew

Impossible I'm afraid. Think about it, a hashcode is a long value i.e. 8 bytes. A string maybe less than this but also could be much longer, you cannot squeeze a longer string into 8 bytes without losing something.

恐怕不可能。想想看,哈希码是一个长值,即 8 个字节。一个字符串可能小于这个,但也可能更长,你不能将一个更长的字符串压缩成 8 个字节而不会丢失一些东西。

The Java hashcode algorithm sums every 8th byte if I remember correctly so you'd lose 7 out of 8 bytes. If your strings are all very short then you could encode them as an int or a long without losing anything.

如果我没记错的话,Java 哈希码算法每 8 个字节求和,所以你会丢失 8 个字节中的 7 个。如果您的字符串都很短,那么您可以将它们编码为 int 或 long 而不会丢失任何内容。

回答by Cephalopod

Let's assume the string consists only of letters, digits and punctuation, so there are about 70 possible characters.

假设字符串仅由字母、数字和标点组成,因此大约有 70 个可能的字符。

log_70{2^32} = 5.22...

log_70{2^32} = 5.22...

This means for any given integer you will find a 5- or 6-character string with this as its hash code. So, retrieving "Hello World": impossible; but "Hello"might work if you're lucky.

这意味着对于任何给定的整数,您都会找到一个 5 或 6 个字符的字符串,并将其作为哈希码。所以,检索"Hello World":不可能;但"Hello"如果你幸运的话可能会奏效。

回答by HyperNeutrino

For example, "1019744689" and "123926772" both have a hashcode of -1727003481. This proves that for any integer, you might get a different result (i.e. reversehashcode(hashcode(string)) != string).

例如,“1019744689”和“123926772”的哈希码都是 -1727003481。这证明对于任何整数,您可能会得到不同的结果(即reversehashcode(hashcode(string)) != string)。