PHP Tutorial - PHP Number Convert






Convert a hexadecimal number to octal number

The following code shows how to convert a hexadecimal number to octal number.



<?php
   $hex = "E196";
   echo base_convert($hex,16,8);
?>

The code above generates the following result.

Convert decimal number to binary number

The following code shows how to convert decimal to binary.



<?php
    echo decbin("3") . "<br>";
    echo decbin("1") . "<br>";
    echo decbin("2014") . "<br>";
    echo decbin("7");
?>

The code above generates the following result.





Convert decimal to hexadecimal

The following code shows how to convert decimal to hexadecimal.



<?php
    echo dechex("30") . "<br>";
    echo dechex("10") . "<br>";
    echo dechex("2014") . "<br>";
    echo dechex("70");
?>

The code above generates the following result.

Convert decimal to octal

The following code shows how to convert decimal to octal.



<?php
    echo decoct("30") . "<br>";
    echo decoct("10") . "<br>";
    echo decoct("2014") . "<br>";
    echo decoct("70");
?>

The code above generates the following result.





Convert binary to decimal

The following code shows how to convert binary to decimal.



<?php
    echo bindec("0011") . "<br>";
    echo bindec("01") . "<br>";
    echo bindec("11000110011") . "<br>";
    echo bindec("111");
?>

The code above generates the following result.

Convert octal to decimal

The following code shows how to convert octal to decimal.


  
<?php
    echo octdec("36") . "<br>";
    echo octdec("12") . "<br>";
    echo octdec("2014") . "<br>";
    echo octdec("106");
?>

The code above generates the following result.

Convert an octal number to a decimal number

The following code shows how to convert an octal number to a decimal number.



<?php
    $oct = "0031";
    echo base_convert($oct,8,10);
?>

The code above generates the following result.

Convert an octal number to a hexadecimal number

The following code shows how to convert an octal number to a hexadecimal number.



<?php
    $oct = "364";
    echo base_convert($oct,8,16);
?>

The code above generates the following result.

Convert hexadecimal to decimal

The following code shows how to convert hexadecimal to decimal.


<?php
    echo hexdec("1e") . "<br>";
    echo hexdec("a") . "<br>";
    echo hexdec("11ff") . "<br>";
    echo hexdec("cceeff");
?>

The code above generates the following result.

Convert hexadecimal values to ASCII characters

The following code shows how to convert hexadecimal values to ASCII characters.


  
<?php
   echo hex2bin("48656c");
?>

The code above generates the following result.