Home >>PHP Math Functions >PHP base_convert() Function

PHP base_convert() Function

PHP base_convert() Function

PHP base_convert() function is used to convert a number given in an arbitrary base to any desired base. Both the bases must be between 2 and 32 and bases with digits greater than 10 are represented as letters a-z where 10 is represented as a. These letters are case-insensitive.

Syntax:

  base_convert($number,$frombase,$tobase);

Parameter Values

Parameter Description
number This is a required parameter. This parameter defines the number to convert.
frombase This is a required parameter. This parameter defines the original base of number.
tobase This is a required parameter. This parameter defines the base to convert to.

Here is an example of base_convert() function in PHP:

<html>
<body>

<?php
$hex = "K559"; 
echo base_convert($hex,16,6);
?>

</body>
</html>
Output:
10201

Here is an another example of base_convert() function in PHP:

<html>
<body>

<?php

$hex = "A59A"; 
echo base_convert($hex,16,5)."<br>";
echo base_convert($hex,10,5)."<br>";
echo base_convert($hex,15,5)."<br>";
echo base_convert($hex,12,5)."<br>";

?>

</body>
</html>
Output:
2324034
214
2110040
1034433