Search code examples
mysqlbinaryhex

Insert hex values into MySql


I have a table with a VARBINARY column. I need to insert a string such as '4D2AFF' which represents the hex values 0x4D, 0x2A and 0xFF respectively. How do I construct this statement?


Solution

  • You can use UNHEX() function to convert a hexstring with hex pairs to binary and HEX() to do the other way round.

    I.e.

    INSERT INTO tbl (col) VALUES (UNHEX('4D2AFF'))
    

    and

    SELECT HEX(col) FROM tbl