The HEX function converts input parameters to hexadecimal string representation. This function is MySQL-compatible and supports both numeric and string input types with different conversion rules.
If the input parameter is a number (BIGINT type), returns the hexadecimal string representation of that number.
If the input parameter is a string, converts each character (by byte) to two hexadecimal characters, then concatenates all converted characters into the result string.
HEX(<expr>)
| Parameter | Description |
|---|---|
<expr> | Input parameter, can be BIGINT type number or VARCHAR type string |
Returns VARCHAR type, representing the hexadecimal representation of the input parameter.
Conversion rules:
Special cases:
SELECT HEX(12), HEX(-1);
+---------+------------------+ | HEX(12) | HEX(-1) | +---------+------------------+ | C | FFFFFFFFFFFFFFFF | +---------+------------------+
SELECT HEX('1'), HEX('@'), HEX('12');
+----------+----------+-----------+ | HEX('1') | HEX('@') | HEX('12') | +----------+----------+-----------+ | 31 | 40 | 3132 | +----------+----------+-----------+
SELECT HEX(255), HEX(65535), HEX(16777215);
+----------+------------+----------------+ | HEX(255) | HEX(65535) | HEX(16777215) | +----------+------------+----------------+ | FF | FFFF | FFFFFF | +----------+------------+----------------+
SELECT HEX(NULL);
+-----------+ | HEX(NULL) | +-----------+ | NULL | +-----------+
SELECT HEX(0), HEX('');
+--------+--------+ | HEX(0) | HEX('') | +--------+--------+ | 0 | | +--------+--------+