SelectHEX(210);+----------+|HEX(210)|+----------+|D2 |+-------- --+1r"/> SelectHEX(210);+----------+|HEX(210)|+----------+|D2 |+-------- --+1r">
Home >Database >Mysql Tutorial >What is the MySQL HEX() function and how is it different from the CONV() function?
Actually, the HEX() function converts a decimal or string value into a hexadecimal value. After conversion, MySQL returns the string representation of the hexadecimal value.
HEX(Num or Str)
We know that the HEX() function can convert numbers or strings, so "Num" in the syntax means the number to be converted to hexadecimal, and "Str" is the number to be converted to hexadecimal. Converts a string number to two hexadecimal characters.
mysql> Select HEX(210); +----------+ | HEX(210) | +----------+ | D2 | +----------+ 1 row in set (0.00 sec)
In the above example, 210 is a decimal number, which is converted to a hexadecimal string representation and treated as a BIGINT number.
mysql> SELECT HEX('NULL'); +-------------+ | HEX('NULL') | +-------------+ | 4E554C4C | +-------------+ 1 row in set (0.00 sec)
In the above example, 'NULL' is a string whose characters are converted to two hexadecimal digits (two hexadecimal digits per character).
Basically, the MySQL HEX() function is equivalent to CONV(N,10,16), but the basic difference is that HEX() can convert string characters into two hexadecimal numbers, but CONV( ) returns 0 when trying to convert string characters to a hexadecimal string. The following example demonstrates -
mysql> Select HEX('N'); +----------+ | HEX('N') | +----------+ | 4E | +----------+ 1 row in set (0.00 sec) mysql> Select CONV('N',10,16); +-----------------+ | CONV('N',10,16) | +-----------------+ | 0 | +-----------------+ 1 row in set (0.00 sec)
The above is the detailed content of What is the MySQL HEX() function and how is it different from the CONV() function?. For more information, please follow other related articles on the PHP Chinese website!