Java recreate string from hashcode

前端 未结 5 1643
眼角桃花
眼角桃花 2020-12-10 15:45

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

e.g. something like this:

String myNewstring = StringUtils.         


        
5条回答
  •  囚心锁ツ
    2020-12-10 16:41

    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.

    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à.

提交回复
热议问题