Nolesh
Nolesh

Reputation: 7018

Get unique integer value from string

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. Each time this value must be the same and depends on the string. I've tried to convert each char of the string to int and then I sum chars to each other. But in case if I have 2 strings with the same set of symbols, it return integer values which are equal to each other. So it doesn't suit me. How can I generate unique integer value from unique string?

UPDATE:

Having considered all given solutions I decided to create function which generate unique integer values. I hope that it excludes collisions.

public int getUniqueInteger(String name){
    String plaintext = name;
    int hash = name.hashCode();
    MessageDigest m;
    try {
        m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1,digest);
        String hashtext = bigInt.toString(10);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < 32 ){
          hashtext = "0"+hashtext;
        }
        int temp = 0;
        for(int i =0; i<hashtext.length();i++){
            char c = hashtext.charAt(i);
            temp+=(int)c;
        }
        return hash+temp;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return hash;
}

Upvotes: 25

Views: 42105

Answers (5)

searching9x
searching9x

Reputation: 1605

You can try with code:

import java.math.BigInteger;

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

thanks.

Upvotes: 3

Adrian Merrall
Adrian Merrall

Reputation: 2499

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.

Upvotes: 17

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

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

Upvotes: 16

jason
jason

Reputation: 241641

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.

Upvotes: 0

Jeff Storey
Jeff Storey

Reputation: 57192

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.

Upvotes: 5

Related Questions