Home  >  Article  >  Backend Development  >  How to convert data to decimal in php

How to convert data to decimal in php

青灯夜游
青灯夜游Original
2021-12-30 18:44:331970browse

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);".

How to convert data to decimal in php

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");
?>

How to convert data to decimal in php

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");
?>

How to convert data to decimal in php

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");
?>

How to convert data to decimal in php

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进制
?>

How to convert data to decimal in php

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!

Statement:
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