Rumah >pembangunan bahagian belakang >tutorial php >十进制转二进制
function dec2bin ($dec) { $flag = array(); while ($dec != 0) { array_push($flag,$dec%2); $dec = (int)($dec/2); } $binstr = ''; while (!empty($flag)) { $binstr .= array_pop($flag); } return $binstr; } echo dec2bin(7);
注意:以上只是为了练习。PHP已经有内嵌函数decbin()以及base_convert();
echo '<br/>'; echo base_convert(7,10,2); echo '<br/>'; echo base_convert(1111,2,8); echo '<br/>'; echo decbin(6);