Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert hex values into MySql

Tags:

mysql

hex

binary

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?

like image 351
Tim Avatar asked Jun 27 '10 02:06

Tim


People also ask

What is hex in MySQL?

HEX() : This function in MySQL is used to return an equivalent hexadecimal string value of a string or numeric Input. If the input is a string then each byte of each character in the string is converted to two hexadecimal digits.

What is 0x in MySQL?

The 0x notation is based on ODBC, for which hexadecimal strings are often used to supply values for BLOB columns.

What does 0x mean in SQL Server?

0x denotes that the value is binary, which is displayed using hex values.

What is binary data type in MySQL?

MySQL's BINARY data type, like CHAR , holds fixed-length strings. You still have to specify the width of the column, e.g. BINARY(20) . The only difference is that MySQL treats the data within the column as raw bytes rather than encoded text.


1 Answers

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 
like image 138
BalusC Avatar answered Sep 19 '22 12:09

BalusC