MariaDB Tutorial MariaDB Advanced MariaDB Database Account Management MariaDB References

MariaDB HEX() Function



The MariaDB HEX() function returns a string containing hexadecimal representation of decimal or string value.

For a string argument str, this function returns a hexadecimal string representation of str where each byte of each character in str is converted to two hexadecimal digits (For Multibyte characters become more than two digits). The inverse of this operation can be performed by using UNHEX() function.

For a numeric argument N, this function returns a hexadecimal string representation of the value of N. This is equivalent to using the CONV() function with CONV(N, 10, 16) syntax. The inverse of this operation can be performed by using the CONV() function with CONV(HEX(N), 16, 10) syntax.

Syntax

/* with string argument */
HEX(str)

/* with numeric argument */
HEX(N)

Parameters

str Required. Specify the string to convert to a hexadecimal representation.
N Required. Specify the number to convert to a hexadecimal representation.

Return Value

Returns the hexadecimal representation of a decimal or string value.

Example 1:

The example below shows the usage of HEX() function.

SELECT X'78797A', HEX('xyz'), UNHEX(HEX('xyz'));
Result: 'xyz', '78797A', 'xyz'

SELECT X'53514C', HEX('SQL'), UNHEX(HEX('SQL'));
Result: 'SQL', '53514C', 'SQL'

SELECT HEX(255), CONV(HEX(255), 16, 10);
Result: 'FF', '255'

SELECT HEX(500), CONV(HEX(500), 16, 10);
Result: '1F4', '500'

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 110
Data 220
Data 330
Data 440
Data 550

The statement given below can be used to get the hexadecimal representation of values of column x.

SELECT *, HEX(x) AS HEX_Value FROM Sample;

This will produce the result as shown below:

DataxHEX_Value
Data 110A
Data 22014
Data 3301E
Data 44028
Data 55032

❮ MariaDB Functions