Related Articles

Related Articles

PHP | base_convert( ) Math Function
  • Last Updated : 04 Nov, 2020

The base_convert() function in PHP is used to convert a number given in an arbitrary base to a desired base. 
Both the base should be between 2 and 32 and bases with digits greater than 10 are represented with letters a-z i.e 10 is represented as a, 11 is represented as b and 35 is represented as z. 
The case of the letters is not sensitive.

Syntax:  

string base_convert($inpNumber, $fromBase, $desBase)

Parameters Used: This function accepts three parameters and are described below: 

  1. $inpNumber : It is the number to be converted.
  2. $fromBase : It is the original base of the number.
  3. $desBase : It is the base to which you want to convert.

Return Value: It returns a string that represents the number converted to the desired base.

Examples: 



Input : base_convert(B296, 16, 8)
Output : 131226

Input : base_convert(B296, 16, 2)
Output : 1011001010010110

Input : base_convert(621, 8, 16)
Output : 191

Input : base_convert(110011, 2, 16)
Output : 33

Below programs illustrate the base_convert() function in PHP: 
 

  • Converting hexadecimal to octal:

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
 
$hexadec = "B296";
echo base_convert($hexadec, 16, 8);
 
?>     

chevron_right


Output: 

131226

  • Converting hexadecimal to binary:

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
 
$hexadec = "B296";
echo base_convert($hexadec, 16, 2);
 
?>     

chevron_right


Output: 

1011001010010110


  • Converting octal to hexadecimal:

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
 
$octal = "621";
echo base_convert($octal, 8, 16);
 
?>  

chevron_right


Output: 

191


  • Converting binary to hexadecimal:

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
 
$binary = "110011";
echo base_convert($binary, 2, 16);
 
?>
      

chevron_right


Output: 

33

Reference
http://php.net/manual/en/function.base-convert.php
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :