How to convert binary to decimal without using PHP native functions?

后端 未结 1 560
慢半拍i
慢半拍i 2021-01-26 18:53

My code is like this :



        
相关标签:
1条回答
  • 2021-01-26 19:33

    Here is in http://php.net/manual/en/function.bindec.php#44910

    function reconvert($bin_nr) {
     $base=1;
     $dec_nr=0;
     $bin_nr=explode(",", preg_replace("/(.*),/", "$1", str_replace("1", "1,", str_replace("0", "0,", $bin_nr))));
     for($i=1; $i<count($bin_nr); $i++) $base=$base*2;
     foreach($bin_nr as $key=>$bin_nr_bit) {
         if($bin_nr_bit==1) {
             $dec_nr+=$base;
             $base=$base/2;
         }
         if($bin_nr_bit==0) $base=$base/2;
     }
     return(array("string"=>chr($dec_nr), "int"=>$dec_nr));
    }
    

    Check here : https://eval.in/556903

    0 讨论(0)
提交回复
热议问题