tags:

views:

1915

answers:

1

Can there any way to insert a hex value into MYSQL? I also want to be able to retreive it in hex form.

For example, something like: INSERT INTO table ( hexTag ) VALUES ( HEX(0x41) );

And if I do this, I want it to put an 'A' into the table

+2  A: 

For that particular use case, you can either insert the hex value directly and it will be interpreted as a string, or use HEX() to input and UNHEX() to output

mysql> create table hexTable(pseudoHex varchar(50));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into hexTable values (0x41);
Query OK, 1 row affected (0.00 sec)

mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| A         |
+-----------+
1 row in set (0.00 sec)

mysql> select HEX(pseudoHex) from hexTable;
+----------------+
| HEX(pseudoHex) |
+----------------+
| 41             |
+----------------+
1 row in set (0.00 sec)

mysql> delete from hexTable;
Query OK, 1 row affected (0.00 sec)

mysql> insert into hexTable values (HEX('A'));
Query OK, 1 row affected (0.00 sec)

mysql> select UNHEX(pseudoHex) from hexTable;
+------------------+
| UNHEX(pseudoHex) |
+------------------+
| A                |
+------------------+
1 row in set (0.00 sec)

mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| 41        |
+-----------+
1 row in set (0.00 sec)

See these links.

Vinko Vrsalovic
When I do insert into hexTable values (0x41);it literally inserts "0x41", did you have to do anything special to get it to convert to 'A'?
nope, standard 5.0.38 console client. How are you trying this? Version/client?
Vinko Vrsalovic