This question scares me! It shows a complete lack of mathematical knowledge!
If you have an N character unicode string then there are 65536^N possible combinations so to be unique you need a number between 0 and 65536^N!
Even if your string only contains ASCII you will still need 0 to 128^N.
You say that your string is long so if I assume 100 characters then you need 0 to 128^100. A VERY VERY VERY ... big range.
MD5 and SHA1 are ways to get a probably unique number but they don't garantee unique.
Roger
if you drop the bit about the integer being fixed length, thenpublic class StrToBI {
public static void main(String [] arg) throws Exception {
String s = "Hello World";
System.out.println(new java.math.BigInteger(s.getBytes("UTF-16")));
}
}
should get you there (although there might be complications involving the more recent unicode specs. involving characters with byte sequences of length >2 (?)
asjf
Create an short array that is 4294967296 ints long. Then reverse the order of your String. Loop over the reversed String and set the corresponding short value to the numeric value if the character. You will then have a fixed length number that is unique for all valid Strings in Java. It will be a fixed length of 8589934592 bytes.
Im sure you could get away with using half that number if you only
want unicode 16 values (no four byte extensions). you can pack two chars
into an int. 4 chars if you are restricted to 8 bit chars.
Then you only need 4294967296 bytes to represent each string uniquely.
look I just saved 4Gig. 7Gig if you use the ASCII approach :P
matfud
> I just have a long string and i want to make a integer
> of fixed length from that string, i thought of using
> hashcode() method to get the integer, but the hashcode
> will not be unique, can one tell me how can i convert
> the string to UNIQUE FIXED LENGTH INTEGER.
>
> Thaning you
> M. Muthu
public class UniqueStringToIntGenerator {
private Vector vec = new Vector();
public int getIntFromString(String s) {
for (int i = 0; i < vec.size(); i++) {
if (vec.get(i).equals(s))
return i;
}
vec.add(s);
return vec.size() - 1;
}
}
P.S. This is meant to be a joke.