PHP Math Function : base_convert()

The base_convert() function is used to converts a number from one number base to another. The syntax is :
 base_convert( number, from_base, to_base );

Where the number specifies the number or value to convert, from_base specifies the original base number, and to_base specifies the base to convert to.

The base number are 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal. The base number has to be between 2 to 36.

Example :
<?php

  $dec = 75;
  echo base_convert($dec, 10, 8) . "<br/>"; // decimal to octal
  echo base_convert($dec, 10, 2) . "<br/>"; // decimal to binary

  $bin = "1111";
  echo base_convert($bin, 2, 16) . "<br/>"; // Binary to hexadecimal
  echo base_convert($bin, 2, 10) . "<br/>"; // Binary to decimal

?>
Output :

113
1001011

f
15


Next Topic :