Home > Database > Mysql Tutorial > What is the MySQL HEX() function and how is it different from the CONV() function?

What is the MySQL HEX() function and how is it different from the CONV() function?

WBOY
Release: 2023-09-06 14:33:08
forward
922 people have browsed it

什么是 MySQL HEX() 函数以及它与 CONV() 函数有何不同?

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.

Grammar

HEX(Num or Str)
Copy after login

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.

Example

mysql> Select HEX(210);
+----------+
| HEX(210) |
+----------+
| D2       |
+----------+
1 row in set (0.00 sec)
Copy after login

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)
Copy after login

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 -

Example

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template