I need to generate a UNIQUE id (int only) from an Alphanumeric string.
e.g. I have security id = 'ABC123DEF' I should be able to generate an unique ID (int only) of "security id" so that the unique ID is always constant.
e.g. Security ID : ABC123DEF Int ID : 9463456892
So that I can store the Int ID in Database and refer the security ID from Int ID anytime.
Some Examples: PBG_CD_20120214_.2 | 201202-CMG188963_T | PBG_TD_20120306_.0001 3 examples :-PIPE seperated
Just use the Java hashing algorithm. Not 100% unique but you can use it as a base and add something to guarantee uniqueness on a much smaller collision set:
public static int hash(String s) {
int h = 0;
for (int i = 0; i < s.length(); i++) {
h = 31 * h + s.charAt(i);
}
return h;
}
In order to avoid collision 100%, you need a prime number that is bigger than the wider difference between your characters. So for 7-bit ASCII, you need something higher than 128. So instead of 31, use 131 (the next prime number after 128). The part I haven't checked is if the generated hash will wind up being bigger than the size of your long ints. But you can take it from there...