Home > Backend Development > PHP Tutorial > PHP hexadecimal to binary code conversion

PHP hexadecimal to binary code conversion

WBOY
Release: 2016-07-25 08:57:57
Original
1391 people have browsed it
  1. var_dump(decto_bin(array(128,253),2));

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

  4. ;

  5. string(8) "10000000"
  6. [1]=>
  7. string(8) "11111101"
  8. }
  9. array(2) {
  10. [0]=>
  11. string(4) "0200"
  12. [1 ]=>
  13. string(4) "0375"
  14. }
  15. array(2) {
  16. [0]=>
  17. string(2) "80"
  18. [1]=>
  19. string(2) "FD"
  20. }

  21. Copy code
2, binary, octal, hexadecimal to decimal This conversion uses multiplication, such as: 1101 to decimal: 1*2^3+1*2^2+0*2^1+1*2^0
Code:

<?php 
/**
 *二进制、八进制、十六进制 转十进制
 * edit by bbs.it-home.org
 *
 * @param array $datalist 传入数据array(df,ef)
 * @param int $bin 转换的进制可以是:2,8,16
 * @return array 返回数据 array() 返回没有数据转换的格式
 */
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; //为10进制不转换
    $aOutData=array(); //定义输出保存数组
    foreach ($datalist as $num)
    {
        $atnum=str_split($num); //将字符串分割为单个字符数组
        $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;
}
?>
Copy after login

Test:

    var_dump(bin_todec(array('ff','ff33','cc33'),16));

  1. var_dump(bin_todec(array('1101101','111101101'), 2));
  2. var_dump(bin_todec(array('1234123','12341'),8));

  3. X-Powered-By: PHP/5.2.0

  4. Content-type: text/html

  5. array(3) {

  6. [0]=>
  7. int(255)
  8. [1]=>
  9. int(65331)
  10. [2]=>
  11. int (52275)
  12. }
  13. array(2) {
  14. [0]=>
  15. int(124)
  16. [1]=>
  17. int(508)
  18. }
  19. array(2) {
  20. [0]=>
  21. int(342099)
  22. [1]=>
  23. int(5345)
  24. }

  25. Copy code
Summary: There are many built-in functions in php that can complete the above base conversion, such as bindec(), decoct(), dechex() base_convert() decbin(), etc. Through this article, I will give you an idea of ​​​​implementing base conversion in PHP. I hope it will be helpful to you.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template