Is there a CPAN module that translates a short string to a short number?

I need to create unique numeric identifiers for some short strings.

some.domain.com -> 32423421 another.domain.com -> 23332423 yet.another.com -> 12131232 

Is there a CPAN Perl module that will do something like this?

I tried using Digest :: MD5, but the resulting numbers are too long:

 some.domain.com -> 296800572457176150356613937260800159845 
+4
source share
3 answers

Just grab the first 8 digits of the MD5 hash. This works because MD5 is evenly distributed over its hash address space . This means that any consecutive MD5 hash sequence itself will be a uniformly distributed hash.

Alternatively, just use another hash distribution mechanism that returns 8 numbers. Whatever is easier for you.

+16
source

Either Digest :: CRC or String :: CRC32 . The first gives you the ability to calculate 8-, 16- and 32-bit chcecksums, and the second only supports 32-bit.

+4
source

Given the fact that the strings look like hostnames, maybe you just allow them for ip and represent ip as a whole?

View like:

 perl -le 'my $ip = gethostbyname("depesz.com"); my $num = unpack("N", $ip); print $num' 1311657670 
+4
source

All Articles