w3resource
MYSQL Tutorial

MySQL UNHEX() function

UNHEX() function

MySQL UNHEX() function performs the opposite operation of HEX(). This function interprets each pair of hexadecimal digits (in the argument) as a number and converts it to a character.

Syntax:

UNHEX (str)

The return value is a binary string.

Argument

Name Description
str A string which is to be converted to a decimal number.

Syntax Diagram:

MySQL UNHEX() Function - Syntax Diagram

MySQL Version: 5.6

Example: MySQL UNHEX() function

mysql> SELECT UNHEX('4d7953514c205475746f7269616c2c77337265736f757263');
+-----------------------------------------------------------+
| UNHEX('4d7953514c205475746f7269616c2c77337265736f757263') |
+-----------------------------------------------------------+
| MySQL Tutorial,w3resourc                                  |
+-----------------------------------------------------------+
1 row in set (0.01 sec)

mysql> SELECT UNHEX(HEX('w3resource'));
+--------------------------+
| UNHEX(HEX('w3resource')) |
+--------------------------+
| w3resource               |
+--------------------------+
1 row in set (0.00 sec)

mysql> SELECT HEX(UNHEX(1234));
+------------------+
| HEX(UNHEX(1234)) |
+------------------+
| 1234             |
+------------------+
1 row in set (0.00 sec)

In UNHEX() function the characters in the argument string must be legal hexadecimal digits i.e. '0'..'9', 'A'.. 'F', 'a'..'f'. If there are nonhexadecimal digits in argument part, the function will return NULL. See the following statement:

mysql> SELECT UNHEX('www');
+--------------+
| UNHEX('www') |
+--------------+
| NULL         |
+--------------+
1 row in set (0.00 sec)

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.