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.
//  w ww.  ja  v a  2  s.c  o m
<?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.
/*from w w w  .j a v a 2s  . c  o  m*/
<?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.
/*ww w . j  a  v  a2 s. c  om*/
<?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.
/* ww w . ja  va  2  s .com*/
<?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.
/*from   www  . jav a2s .  c o  m*/
<?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.
  /*from   w  ww.  java 2s  .  c  o  m*/
<?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.
/*w w  w. ja v a  2 s. co m*/
<?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.
/*from  w w  w.ja v a 2  s .  c  om*/
<?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//  w w w.  java2  s .c o m
    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.
Comments
Post a Comment