25

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;
}
4
  • Why not use getBytes to convert each character into its ASCII equivalent? Then just sum those. Commented Jul 11, 2013 at 1:25
  • 3
    Use String#hashCode()? Commented Jul 11, 2013 at 1:29
  • 2
    hashCode does not guarantee the uniqueness! Commented Jul 11, 2013 at 2:39
  • @DevlshOne That does not differentiate "aabc" from "cbaa" Commented May 30, 2016 at 12:44

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

Comments

16

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

1 Comment

@Nolesh No solution does.
5

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.

1 Comment

It's not clear how to get the integer value, as the function returns an array of bytes
3

You can try with code:

import java.math.BigInteger;

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

thanks.

1 Comment

What is the memory consumption for this compared to a String?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.