Get unique integer value from string

前端 未结 5 1761
我在风中等你
我在风中等你 2021-02-07 06:34

I have different unique strings in the same format. The string looks like this axf25!j&809>-11~dc and I want to get unique integer value from this string.

相关标签:
5条回答
  • 2021-02-07 06:51

    You could just use String.hashCode() (e.g. mystring.hashCode()) to give you a degree of uniqueness but you must ensure you can handle collisions.

    0 讨论(0)
  • 2021-02-07 06:55

    You can't guarantee unique integer values from different strings since there are more possible string representations than integers. You can use some well known/defined hashing algorithm to minimize the chance of a collision. You should look at MD5 or SHA.

    The java class MessageDigest should be of some use.

    0 讨论(0)
  • 2021-02-07 06:59

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

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

    0 讨论(0)
  • 2021-02-07 07:04

    You can try with code:

    import java.math.BigInteger;
    
    public static BigInteger stringToBigInteger(String text) {
        BigInteger bigInt = new BigInteger(text.getBytes());
        return bigInt;
    }
    

    thanks.

    0 讨论(0)
  • 2021-02-07 07:05

    Treat the strings as a base 0x110000 representation of some integer (you can get away with a smaller base if you know the range of characters is limited). Convert to a BigInteger.

    0 讨论(0)
提交回复
热议问题