Home > Article > Backend Development > How to convert data to decimal in php
Conversion method: 1. Use "bindec(value)" to convert binary to decimal; 2. Use "octdec(value)" to convert octal to decimal; 3. Use "hexdec(value) )", you can convert hexadecimal to decimal; 4. Use "base_convert(value, original base, 10);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php converts data For decimal
1, use the bindec() function--binary number to decimal number
bindec() function converts a binary number to a decimal number.
<?php echo bindec("0011") . "<br>"; echo bindec("01") . "<br>"; echo bindec("11000110011") . "<br>"; echo bindec("111"); ?>
2. Use the octdec() function--octal number to decimal number
octdec() function to convert octal number is a decimal number.
<?php echo octdec("36") . "<br>"; echo octdec("12") . "<br>"; echo octdec("3063") . "<br>"; echo octdec("106"); ?>
3. Use the hexdec() function--hexadecimal number to decimal number
hexdec() function Convert hexadecimal number to decimal number.
<?php echo hexdec("1e") . "<br>"; echo hexdec("a") . "<br>"; echo hexdec("11ff") . "<br>"; echo hexdec("cceeff"); ?>
4. Use the base_convert() function--convert between arbitrary bases
base_convert() function in any base Convert numbers between.
Syntax:
base_convert(number,frombase,tobase);
Parameters | Description |
---|---|
number | Required. Specifies the number to be converted. |
frombase | Required. Specifies the original base of the number. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35. |
tobase | Required. Specifies the base to be converted. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35. |
Example: Convert other bases to decimal
<?php echo base_convert("cceeff",16,10) . "<br>";//16进制转10进制 echo base_convert("3063",8,10) . "<br>";//8进制转10进制 echo base_convert("11000110011",2,10);//2进制转10进制 ?>
Recommended study: "PHP Video Tutorial》
The above is the detailed content of How to convert data to decimal in php. For more information, please follow other related articles on the PHP Chinese website!