Wednesday, 17 August 2016

MYSQL - Convert/encode string to numbers

This will work for strings up to 8 characters long.
To encode:
SELECT CONV(HEX(string), 16, 10);
To decode:
SELECT UNHEX(CONV(number, 10, 16));
MySQL supports integers up to 64 bit long, and this method uses 8 bits per character. Therefore using this method you can store up to 64 / 8 = 8 characters in an integer.


SELECT CONV(HEX('testoabc'), 16, 10) as encode,UNHEX(CONV(8387236824969470563, 10, 16)) as decode;

encodedecode
8387236824969470563testoabc


0 comments:

Post a Comment