Re: How to pass a hexadecimal value in a binary mask?
Date: March 02, 2011 05:11AM
I finally found the solution by myself.
Here what the documentation says about the HEX() function :
"For a string argument str, HEX() returns a hexadecimal string representation of str where each character in str is converted to two hexadecimal digits. The inverse of this operation is performed by the UNHEX() function.
For a numeric argument N, HEX() returns a hexadecimal string representation of the value of N treated as a longlong (BIGINT) number. This is equivalent to CONV(N,10,16). The inverse of this operation is performed by CONV(HEX(N),16,10).
mysql> SELECT 0x616263, HEX('abc'), UNHEX(HEX('abc'));
-> 'abc', 616263, 'abc'
mysql> SELECT HEX(255), CONV(HEX(255),16,10);
-> 'FF', 255
HEX() and BIN() returns the representation of the hexadecimal and binary value. So, write 0x616263 is different to write HEX('abc') because when you want to work with the mask, the result is not the same :
- "SELECT 0x616263 & 255" is an equivalent to "SELECT 6382179 & 255" in decimal and returns 99.
- "SELECT HEX('abc') & 255" is an equivalent to "SELECT 616263 & 255" in decimal and returns 71.
Finally, if you want to perform a subsearch on a value from the database, you must convert it in decimal value before, in doing this :
- "SELECT CONV(HEX('abc'), 16, 10) & 255" returns 99 (either the "c" character).
Hoping this can help.