1. Computing & Technology

Discuss in my forum

Base_convert () PHP Function

By , About.com Guide

What is a base?:

The base we are the most familiar with (our regular numeric system) is base 10. What this means is that, each 'slot' has 10 possibilities before a second digit is needed. For example we can have the digits 0-9 before needing to add a second digit. In binary, there are only two possibilities (0 or 1) before adding another digit. So, zero is 0 and one is 1, but two is written as 10, and three as 11.

Converting base:

In PHP we can convert base simply using the base_convert () function. We need to specify the number, its current base, and the base we want to convert to. For example, converting 100 from base 10 to base 2 would be: base_convert (100, 10, 2)

Examples:

 <?php 
 //Converting from base 2 and base 16 to base 10

 $bi= '101101';
 echo base_convert($bi, 2, 10) ;

 $hex= 'A37443';
 echo base_convert($hex, 16, 10) ;
 ?> 

©2012 About.com. All rights reserved.

A part of The New York Times Company.