Home >Backend Development >PHP Problem >How to convert hexadecimal and decimal to each other in php
Methods for mutual conversion: 1. Use the "hexdec (hexadecimal string)" statement to convert hexadecimal to decimal; 2. Use the "dechex (decimal value)" statement , can convert decimal to hexadecimal value; 3. Use the "base_convert("base value", original base, target base)" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
16 hexadecimal and Conversion between decimal numbers
1. To convert hexadecimal numbers to decimal numbers
, you can use hexdec(hexdec) Hexadecimal string)
function, which can convert hexadecimal numbers into decimal numbers.
<?php echo hexdec("1e") . "<br>"; echo hexdec("a") . "<br>"; echo hexdec("11ff") . "<br>"; echo hexdec("cceeff"); ?>
Output result:
You can also use the base_convert() function, just set "bindec(hexadecimal string, 16 , 10)
" is enough.
<?php echo base_convert("1e", 16, 10) . "<br>"; echo base_convert("a", 16, 10) . "<br>"; echo base_convert("11ff", 16, 10) . "<br>"; echo base_convert("cceeff", 16, 10); ?>
Output result:
30 10 4607 13430527
2. Convert decimal number to hexadecimal value
You can use dechex (decimal value)
Function, which converts decimal numbers to hexadecimal numbers.
<?php echo dechex("30") . "<br>"; echo dechex("10") . "<br>"; echo dechex("1587") . "<br>"; echo dechex("70"); ?>
Output result:
You can also use the base_convert() function, just set "bindec(decimal value, 10, 16)
" is all.
<?php echo base_convert("30", 10, 16) . "<br>"; echo base_convert("10", 10, 16) . "<br>"; echo base_convert("1587", 10, 16) . "<br>"; echo base_convert("70", 10, 16); ?>
Output results:
1e a 633 46
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert hexadecimal and decimal to each other in php. For more information, please follow other related articles on the PHP Chinese website!