PHP | base_convert( ) Math Function
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:
- $inpNumber : It is the number to be converted.
- $fromBase : It is the original base of the number.
- $desBase : It is the base to which you want to convert.
Return Value: It returns a string which reperesents 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$hexadec="B296";echo base_convert($hexadec, 16, 8);?>chevron_rightfilter_noneOutput:
131226
- Converting hexadecimal to binary:
<?php$hexadec="B296";echo base_convert($hexadec, 16, 2);?>chevron_rightfilter_noneOutput:
1011001010010110
- Converting octal to hexadecimal:
<?php$octal="621";echo base_convert($octal, 8, 16);?>chevron_rightfilter_noneOutput:
191
- Converting binary to hexadecimal:
<?php$binary="110011";echo base_convert($binary, 2, 16);?>chevron_rightfilter_noneOutput:
33
Reference:
http://php.net/manual/en/function.base-convert.php
Recommended Posts:
- HTML5 | MathML <math> tag
- JavaScript | Math Object
- PHP | Math Functions Complete Reference
- JavaScript | Math Object Complete Reference
- PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
- PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Map get() Function
- D3.js | d3.min() function
- p5.js | second() function
- p5.js | abs() function
- D3.js | d3.sum() function
- p5.js | sq() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



