且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串中获取唯一的整数值

更新时间:2023-11-06 18:42:28

你无法生成完全独特的 int 来自足够长的字符串因为还有10个字符字符串而不是32位整数

You cannot generate entirely unique ints from sufficiently long strings because there are more 10-character strings than 32-bit integers.

就非唯一解决方案而言,你可以使用标准的 hashCode 函数,它在Java中的实现相当不错。对于更复杂的内容,您可以考虑计算加密哈希值( SHA-2 MD5 等。)

As far as non-unique solutions go, you can use the standard hashCode function, its implementation in Java is reasonably good. For more complex stuff you may consider computing cryptographic hash (SHA-2, MD5, etc.)