Why does this MySQL XOR query return 0?

151 views Asked by At

I am trying to compute the hamming distance between two hex strings. First, the strings are converted from base 16 to base 10, then they are xor'd and the bits are counted:

SELECT (CONV('b4124b0d195b2507', 16, 10)) ^ (CONV('eae26aebf1f139f9', 16, 10));

This results in 0.

Independently running

SELECT (CONV('b4124b0d195b2507', 16, 10));

and

SELECT (CONV('eae26aebf1f139f9', 16, 10));

give me the answers I would expect (12975515996039881991 and 16925207911220722169).

Where is the flaw in my logic?

1

There are 1 answers

1
exussum On BEST ANSWER
 SELECT CONVERT((CONV('b4124b0d195b2507', 16, 10)), SIGNED) ^ CONVERT((CONV('eae26aebf1f139f9', 16, 10)), SIGNED)

is what you want

The conv as per the docs

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_conv

Returns a string representation of the number N, converted from base from_base to base to_base

You need to convert back to numbers to xor