php將二進(jìn)制轉(zhuǎn)換為十進(jìn)制的方法:1、使用bindec()函數(shù),語(yǔ)法“bindec($binary_string)”;2、使用base_convert()函數(shù),語(yǔ)法“base_convert($binary_string,2,10)”。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php將二進(jìn)制轉(zhuǎn)換為十進(jìn)制
1、使用bindec()函數(shù)
bindec($binary_string)
函數(shù)把二進(jìn)制數(shù)$binary_string
轉(zhuǎn)換為十進(jìn)制數(shù)。
<?php echo bindec("0011") . "<br>"; echo bindec("01") . "<br>"; echo bindec("11000110011") . "<br>"; echo bindec("111"); ?>
輸出結(jié)果:
2、使用base_convert()函數(shù)
base_convert() 函數(shù)在任意進(jìn)制之間轉(zhuǎn)換數(shù)字。
base_convert($binary_string,$frombase,$tobase);
-
$binary_string 必需。規(guī)定要轉(zhuǎn)換的二進(jìn)制數(shù)。
-
$frombase 必需。規(guī)定數(shù)字原來的進(jìn)制。介于 2 和 36 之間(包括 2 和 36)。高于十進(jìn)制的數(shù)字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。
-
$tobase 必需。規(guī)定要轉(zhuǎn)換的進(jìn)制。介于 2 和 36 之間(包括 2 和 36)。高于十進(jìn)制的數(shù)字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。
<?php header("content-type:text/html;charset=utf-8"); $num1="0011"; $num2="11000110011"; echo "二進(jìn)制 ".$num1." 轉(zhuǎn)換成十六進(jìn)制 ".base_convert($num1,2,10)."<br>"; echo "二進(jìn)制 ".$num2." 轉(zhuǎn)換成十六進(jìn)制 ".base_convert($num2,2,10)."<br>"; ?>
輸出結(jié)果:
推薦學(xué)習(xí):《PHP視頻教程》