简体   繁体   中英

Hash algorithm to put a specific number of digits into a fixed number of buckets

I want to cram 10 base 10 digits (0 to 9,999,999,999) into 6 hex digits (00 00 00 to FF FF FF). I know there will be collisions and that's fine. I also don't need to worry about a separate chain for the collisions to be housed in. Also, I don't need to convert from the hash back into the original. Just need to have a set number of buckets (6 hex digits in 3 bytes) that can be represented as 00 00 00 to FF FF FF. So yes, there will be many numbers in 0 to 9,999,999,999 that map to those buckets...that's fine.

Does anyone know of a good algorithm to do that or perhaps a package in Java/Kotlin?

The simplest approach would be to simply use mod on the base 10 number

base_var_value % 16777215

That 16M number is simply FF FF FF in hex.

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