I have different unique lines in the same format. The line looks like axf25!j&809>-11~dc , and I want to get a unique integer value from this line. Each time this value should be the same and depends on the string. I tried converting each char string to int, and then I sum the characters to each other. But in case I have 2 lines with the same set of characters, they return integer values ββequal to each other. So this does not suit me. How can I generate a unique integer value from a unique string?
UPDATE:
Having considered all the solutions provided, I decided to create a function that generates unique integer values. I hope this rules out 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; }
java string type-conversion integer
Nolesh Jul 11 '13 at 1:22 on 2013-07-11 01:22
source share