Java recreate string from hashcode

前端 未结 5 1668
眼角桃花
眼角桃花 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:33

    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.

    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.

提交回复
热议问题