I’ve put together a PHP function as follows:
function keyword_hash($keyword) {
return base_convert(substr(md5($keyword), -16), 16, 10);
}
The aim of this function is to generate a numeric hash value that I can store in a database, and use that for a look up (rather than trying to index a keyword column).
The equivalent of this function in MySQL is as follows:
SELECT CONV(RIGHT(MD5('some keyword'), 16), 16, 10);
I’ve verified that the MD5 string is the same, and the substr() matches the value I get back from RIGHT() in the MySQL query. However, when I run CONV(), I’m getting a different value to what’s generated from base_convert().
For example, using keyword_hash("some keyword") generates a value of 10923672322315740844. However, using SELECT CONV(RIGHT(MD5('some keyword'), 16), 16, 10) generates 10923672322315740475, which shows the last three numbers as being different.
What am I missing here? Shouldn’t they produce the same value?
I took a look at the PHP Manual page for
base_convert(). There is the following warning:Later in the comments someone already found a solution for this problem (thanks @CraigSefton):
This function uses the bc math library which supports arbitrary precision mathematics because it uses strings to store the numbers instead of integers/floats etc.