PHP base convert

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : base_convert()  function

Description

The base_convert() function is used to convert a number from one base to another.

Version

(PHP 4 and above)

Syntax

base_convert (cstring, initial_base, change_base )

Parameters

Name

Description

Required /
Optional

Type

cstring

The number to convert.

Required

String

initial_base

The initial base.

Required

integer

new_base

The base to convert to.

Required

integer

Note : Both initial_base and new_base have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented by the letters a-z, with a meaning 10, b meaning 11 and z meaning 35.

Return value

cstring converted to base new_base.

Value Type : String.

Example :

In the following example base_convert() function converts an hexa number to octal number.

  1. $hexa = 'E164';  

  2. echo base_convert($hexa, 16, 8);  

  3. ?>  

Output :

160544