java - Get unique integer value from string -
i have different unique strings in same format. string looks axf25!j&809>-11~dc
, want unique integer value string. each time value must same , depends on string. i've tried convert each char of string int , sum chars each other. in case if have 2 strings same set of symbols, return integer values equal each other. doesn't suit me. how can generate unique integer value unique string?
update:
having considered given solutions decided create function generate unique integer values. hope 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); // need 0 pad if want full 32 chars. while(hashtext.length() < 32 ){ hashtext = "0"+hashtext; } int temp = 0; for(int =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; }
you cannot generate entirely unique int
s sufficiently long strings because there more 10-character strings 32-bit integers.
as far non-unique solutions go, can use standard hashcode
function, implementation in java reasonably good. more complex stuff may consider computing cryptographic hash (sha-2, md5, etc.)
Comments
Post a Comment