Home >Database >Mysql Tutorial >Using hexadecimal numbers in MySQL?
To use hexadecimal, use the CONV() function to convert between bases. The syntax is as follows -
SET anyVariableName = CONV(yourHexValue,16,10);
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 ;
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;
Use the CALL command to call the above stored procedure. The query is as follows -
mysql> call SP_HEX_TO_DEC('A');
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)
Check directly with select statement.
mysql> select conv('AB',16,10) as DecimalResult;
The following is the output -
+---------------+ | DecimalResult | +---------------+ | 171 | +---------------+ 1 row in set (0.00 sec)
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.
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!