简体   繁体   中英

Turn string to unique id between 2 numbers

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM