MySQL has a few built-in functions for handling binary data. One of them is HEX which converts any data into hexadecimal representation. The function which you would expect to do the opposite (as the manual states) is UNHEX which takes a hexadecimal representation and turns it into characters.
So, if you try to do:
SELECT UNHEX(HEX('a'));You get an "a" back. But if you try that on a integer:
SELECT UNHEX(HEX(1));You get back the char corresponding to the ASCII value of 1, which is not what was intended.
The correct way to do it, and the real opposite of HEX is the CONV function which converts betweens bases:
SELECT CONV(HEX(1), 16, 10);This time the result is the number "1" as expected.