SoFunction
Updated on 2025-03-03

PHP implements mutual conversion of the system

Converting from decimal to other binary systems is to use the number to continuously divide by the number to be converted to read the remainder. Just connect it together.

<?php 
/**
  *Decimal to binary, octal, hexadecimal, and zeros are added before the number of insufficient digits*
  *
  * @param array $datalist Incoming data array(100,123,130)
  * @param int $bin The conversion of the binary can be: 2,8,16
  * @return array Returns data array() Returns format without data conversion
  */
function decto_bin($datalist,$bin)
{
  static $arr=array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F');
  if(!is_array($datalist)) $datalist=array($datalist);
  if($bin==10)return $datalist; //Same Category Ignore  $bytelen=ceil(16/$bin); //Get the length of one byte if it is $bin  $aOutChar=array();
  foreach ($datalist as $num)
  {
    $t="";
    $num=intval($num);
  if($num===0)continue;
    while($num>0)
    {
      $t=$arr[$num%$bin].$t;
      $num=floor($num/$bin);
    }
    $tlen=strlen($t);
    if($tlen%$bytelen!=0)
    {
    $pad_len=$bytelen-$tlen%$bytelen;
    $t=str_pad("",$pad_len,"0",STR_PAD_LEFT).$t; // Less than one byte length, 0 will be added automatically in front of it.    }
    $aOutChar[]=$t;
  }
  return $aOutChar;
}

test:

var_dump(decto_bin(array(128,253),2));
var_dump(decto_bin(array(128,253),8));
var_dump(decto_bin(array(128,253),16));

X-Powered-By: PHP/5.2.0
Content-type: text/html

array(2) {
 [0]=>
 string(8) "10000000"
 [1]=>
 string(8) "11111101"
}
array(2) {
 [0]=>
 string(4) "0200"
 [1]=>
 string(4) "0375"
}
array(2) {
 [0]=>
 string(2) "80"
 [1]=>
 string(2) "FD"
}

Binary, octal, hexadecimal to decimal

This conversion is multiplication, such as: 1101 to decimal: 1*2^3+1*2^2+0*2^1+1*2^0

Code:

<?php 
/**
  *Binary, octal, hexadecimal to decimal*
  *
  * @param array $datalist Pass in data array(df,ef)
  * @param int $bin The conversion of the binary can be: 2,8,16
  * @return array Returns data array() Returns format without data conversion
  * @copyright chengmo QQ:8292669
  */
function bin_todec($datalist,$bin)
{
  static $arr=array('0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15);
  if(!is_array($datalist))$datalist=array($datalist);
  if($bin==10)return $datalist; //No conversion for decimal  $aOutData=array(); //Define the output to save the array  foreach ($datalist as $num)
  {
    $atnum=str_split($num); //Split the string into a single character array    $atlen=count($atnum);
    $total=0;
    $i=1;
    foreach ($atnum as $tv)
    {
      $tv=strtoupper($tv);
       
      if(array_key_exists($tv,$arr))
      {
        if($arr[$tv]==0)continue;
        $total=$total+$arr[$tv]*pow($bin,$atlen-$i);
      }
      $i++;
    }
    $aOutData[]=$total;
  }
  return $aOutData;
}
 

test:

var_dump(bin_todec(array('ff','ff33','cc33'),16));
var_dump(bin_todec(array('1101101','111101101'),2));
var_dump(bin_todec(array('1234123','12341'),8));

X-Powered-By: PHP/5.2.0
Content-type: text/html

array(3) {
 [0]=>
 int(255)
 [1]=>
 int(65331)
 [2]=>
 int(52275)
}
array(2) {
 [0]=>
 int(124)
 [1]=>
 int(508)
}
array(2) {
 [0]=>
 int(342099)
 [1]=>
 int(5345)
}

These are just implementation methods, but in fact, they don’t care about the php language or other things, and the implementation ideas are the same. PHP actually has many built-in functions to complete these contents:

bindec(), decoct(), dechex() base_convert() decbin() Here is just an implementation idea. hehe!