Skip to main content

UNHEX

Description

The unhex function is used to convert a hexadecimal string back into the original string. It converts every two hexadecimal characters into one byte. When an invalid value is passed as a parameter, it will return empty string. The unhex_null function has the same effect as the unhex function. However, when an invalid value is passed as a parameter, it will return NULL.

tip

unhex_null function is supported since version 3.0.6.

Syntax

UNHEX(<str>)

Parameters

ParameterDescription
<str>The hexadecimal character string

Return Value

If the input string has a length of 0 or is odd, unhex function returns an empty string while unhex_null function returns NULL. If the string contains characters other than [0-9], [a-f], or [A-F], unhex function returns an empty string while unhex_null function returns NULL. In other cases, every two characters are converted to their hexadecimal representation and concatenated into a string for output.

Examples

select unhex('@');
+------------+
| unhex('@') |
+------------+
| |
+------------+
select unhex_null('@');
+-----------------+
| unhex_null('@') |
+-----------------+
| NULL |
+-----------------+
select unhex('41');
+-------------+
| unhex('41') |
+-------------+
| A |
+-------------+
select unhex('4142');
+---------------+
| unhex('4142') |
+---------------+
| AB |
+---------------+