base_convert():Converting from one base to other

Convert a number between bases

base_convert() converts a number represented as a string from one base to another. PHP supports bases from 2 through 36; the returned value is also a string.

$hex = 'ff';
echo base_convert($hex, 16, 10); // 255
echo base_convert('255', 10, 2); // 11111111

Start with this current pattern. The examples below are retained for additional variations, output and reference.

echo base_convert(4,10,2); // Output 100 
echo "<br>";
echo base_convert(10,10,2); // Output 1010
Syntax
string base_convert(string $number, int $from_base, int $to_base);
ParameterDESCRIPTION
$numberRequired : Input string representing number to convert. With a base higher than 10 is represented by Char.
Here a is 10, b is 11 ... so z is 35. ( case in-sensitive )
$from_baseRequired : ( between 2 and 36 , inclusive )
$to_baseRequired : ( between 2 and 36 , inclusive )
The output is number converted to $to_base base.

Example : to Decimal to Binary

echo base_convert(6,10,2); // Output 110 
echo "<br>";
echo base_convert(42,10,2); // Output 101010 

Example : Decimal to Octal

echo base_convert(10,10,8); // Output is 12
echo "<br>";
echo base_convert('15',10,8); // Output is 17

Example : Decimal to Hex

echo base_convert(12,10,16); // Output is c
echo "<br>";
echo base_convert('25',10,16); // Output is 19

Example : Octal to Binary

echo base_convert(13,8,2); // Output is 1011
echo "<br>";
echo base_convert('25',8,2); // Output is 10101

Example Octal to Decimal

echo base_convert(14,8,10); // Output is 12
echo "<br>";
echo base_convert(27,8,10); // Output is 23

Example Octal to Hex

echo base_convert(15,8,16); // Output is d
echo "<br>";
echo base_convert(30,8,16); // Output is 18

Example Hex to Binary

echo base_convert(15,16,2); // Output is 10101
echo "<br>";
echo base_convert(31,16,2); // Output is 110001

Example Hex to Octal

echo base_convert(14,16,8); // Output is 24
echo "<br>";
echo base_convert(32,16,8); // Output is 62

Example Hex to Decimal

echo base_convert(13,16,10); // Output is 19
echo "<br>";
echo base_convert(33,16,10); // Output is 51
Math Functions bindec(): Decimal equivalent of Binary string decbin() Binary equivalent of decimal number


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer