MySQL8 HEX() Functions – String Functions

MySQL HEX() Functions: Syntax

HEX ( string1 );
or
HEX ( N );
 
string1 represents input as a string.
N represents input as a number.

MySQL HEX() Functions: Description

Hex() function returns a hexadecimal representation of decimal or string value.

    • If the argument is a string argument then, HEX() function returns a hexadecimal string representation of string1 where each byte of each character in string1 is converted to two hexadecimal digits. (Multibyte characters, therefore, become more than two digits.)
    • We can use the UNHEX()  function to inverse the original value converted by HEX() function. This will work for string arguments.
    • If the argument is a numeric argument then, Hex() function returns a hexadecimal string representation of the value of N treated as a longlong (BIGINT) number. This is equivalent to CONV(HEX(N),16,10). The inverse of this operation is performed by CONV(HEX(N),16,10).

MySQL HEX() Functions: Parameter

[table caption=”” width=”100%” colwidth=”15%|30%|55%” colalign=”left|left|left”]
Name, Required /Optional, Description
string1, Required, It represents input as a string.
N, Numeric, It represents input as a number.

[/table]


MySQL HEX() Functions: Output

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns, hexadecimal representation of decimal or string value.
[/table]


MySQL HEX() Functions: Available from

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 5.7
[/table]


MySQL HEX() Functions: Example 1

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

MySQL HEX() Functions: Example 2

Shows string as input.

mysql>  SELECT X'74757473', HEX('tuts'), UNHEX(HEX('tuts'));
+-------------+-------------+--------------------+
| X'74757473' | HEX('tuts') | UNHEX(HEX('tuts')) |
+-------------+-------------+--------------------+
| tuts        | 74757473    | tuts               |
+-------------+-------------+--------------------+
1 row in set (0.00 sec)

MySQL HEX() Functions: Example 3

Shows numbers as input. As, u can see in this example, UNHEX() doesn’t inverse HEX() numeric values.

mysql> SELECT HEX(155), CONV(HEX(155),16,10) , unhex(hex(155));
+----------+----------------------+-----------------+
| HEX(155) | CONV(HEX(155),16,10) | unhex(hex(155)) |
+----------+----------------------+-----------------+
| 9B       | 155                  | �                |
+----------+----------------------+-----------------+
1 row in set (0.00 sec)

See all MySQL String functions MySQL 8 String Functions.


Related articles : UNHEX(), CONCAT(), CONCAT_WS() , LOWER(), UPPER(), LTRIM(), RTRIM().


PHP Related articles : HEX2BIN(), BIN2HEX(), PHP STRING FUNCTIONS().

You may also like...