为了使用十六进制,请使用 conv() 函数在基数之间进行转换。语法如下 -
set anyvariablename = conv(yourhexvalue,16,10);
为了理解上述语法,让我们创建一个存储过程。创建存储过程的查询如下 -
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 ;
上面的存储过程将十六进制转换为十进制。我们知道 a 代表十进制的 10,因此我们将 a 作为参数传递。使用call命令调用存储过程。
语法如下 -
call yourstoredprocedurename;
使用 call 命令调用上述存储过程。查询如下 -
mysql> call sp_hex_to_dec('a');
以下是显示使用上面创建的存储过程计算的十进制值的输出 -
+--------------+| decimalvalue |+--------------+| 10 |+--------------+1 row in set (0.00 sec)query ok, 0 rows affected (0.01 sec)
直接用select语句检查。
mysql> select conv('ab',16,10) as decimalresult;
以下是输出 -
+---------------+| decimalresult |+---------------+| 171 |+---------------+1 row in set (0.00 sec)
现在让我们看看将十六进制转换为十进制的过程。记住这条规则 -
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.
以上就是在 mysql 中使用十六进制数字?的详细内容。