Home > Database > Mysql Tutorial > body text

Using hexadecimal numbers in MySQL?

PHPz
Release: 2023-09-13 18:45:02
forward
1127 people have browsed it

在 MySQL 中使用十六进制数字?

To use hexadecimal, use the CONV() function to convert between bases. The syntax is as follows -

SET anyVariableName = CONV(yourHexValue,16,10);
Copy after login

To understand the above syntax, let us create a stored procedure. The query to create the stored procedure is as follows -

mysql> DELIMITER //
mysql> CREATE PROCEDURE SP_HEX_TO_DEC( HEXVALUE VARCHAR(10) )
   -> BEGIN
   -> DECLARE Decimalvalue INTEGER;
   -> SET Decimalvalue = CONV(HEXVALUE,16,10);
   -> select Decimalvalue;
   -> END;
   -> //
Query OK, 0 rows affected (0.19 sec)
mysql> DELIMITER ;
Copy after login

The above stored procedure converts hexadecimal to decimal. We know that A represents 10 in decimal, so we pass A as the parameter. Use the CALL command to call stored procedures.

The syntax is as follows -

CALL yourStoredProcedureName;
Copy after login

Use the CALL command to call the above stored procedure. The query is as follows -

mysql> call SP_HEX_TO_DEC('A');
Copy after login

The following is the output showing the decimal value calculated using the stored procedure created above -

+--------------+
| Decimalvalue |
+--------------+
| 10           |
+--------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Copy after login

Check directly with select statement.

mysql> select conv('AB',16,10) as DecimalResult;
Copy after login

The following is the output -

+---------------+
| DecimalResult |
+---------------+
| 171           |
+---------------+
1 row in set (0.00 sec)
Copy after login

Now let us see the process of converting hexadecimal to decimal. Remember this rule -

A and B represented as 10 and 11 respectively in hexadecimal.
To convert it into decimal rule is as follows:
N ………+value3 *162 +value2 *161 + value1 * 160
= 10 * 161 + 11 * 160
= 160+11
= 171.
Copy after login

The above is the detailed content of Using hexadecimal numbers in MySQL?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!