base_convert() function in PHP


The base_convert() function converts a number from one base to another, for example, octal number to decimal number. The base mentioned here should be between 2 and 36. Digits in numbers with a base greater than 10 is represented with the letters a-z i.e a is 10, , d is 13, z is 35, etc.

Syntax

base_convert(num, original_base, to_base)

Parameters

  • num − The number to convert

  • original_base − The original base. The base mentioned here should be between 2 and 36. Digits in numbers with a base greater than 10 is represented with the letters a-z i.e a is 10, , d is 13, z is 35, etc.

  • to_base − The base to convert to. The base mentioned here should be between 2 and 36. Digits in numbers with a base greater than 10 is represented with the letters a-z i.e a is 10, , d is 13, z is 35, etc.

Return

The base_convert() function returns a string representing the number converted to the desired base.

Example

 Live Demo

<?php
   $res = "0040";
   echo base_convert($res,8,10);
?>

Output

32

Example

 Live Demo

<?php
   $res = "D365";
   echo base_convert($res,16,8);
?>

Output

151545

Example

Let us see another example −

 Live Demo

<?php
   $res = "101101";
   echo base_convert($res,2,16);
?>

Output

2d

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Dec-2019

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements