Home > Net >  Turn string to unique id between 2 numbers
Turn string to unique id between 2 numbers

Time:10-30

How would you convert a string to a unique integer between two values? In a way that it would always return the same number for the same string

For example if i had this string "Hello world" it would need to be converted to an unique number between 0 and 15. And then if the string was different it would be converted to a different number between 0 and 15 but the same string would always convert to the same number

You can get the hash code which is just an integer from a string in java with string.hashCode(). Maybe that could be used to generate it somehow.

CodePudding user response:

string.hashCode() % 15; would get you a number in [0, 14]. If 15 needs to be an option, then you could do string.hashCode() % 16; instead.

  • Related