cedivad cedivad - 3 months ago 24
MySQL Question

PHP hashing function that returns an integer (32bit int)

The goal is to store the hash on a mysql database, using INT (not BIGINT or MEDIUMINT).

md5('string', true)
returns binary data, 16 bytes of hash. I thought i could grep the first 4 bytes and convert it to an INT(32bit/4bytes) integer, but i don't know how to do it.

What do you suggest? Thanks.

Answer
ord($hash[0]) * 16777216 + ord($hash[1]) * 65536 + ord($hash[2]) * 256 + ord($hash[3]) ;

Or:

unpack("L", substr($hash,0,4));

But Frosty Z's solution is better.

Comments