Last Updated:

Conversion to various number systems : PHP

Syntax:

string base_convert (string $number, int $frombase, int $tobase)


Translates a number of $number (specified as a string in the base $frombase number system) into a base $tobase system. The $frombase and $tobase parameters can only take values from 2 to 36, inclusive. In the line $number, the numbers denote themselves, and the letter a corresponds to 11, b -12, etc. to z, which stands for 36. For example, the following commands will output 11111111 (8 units) because this is nothing more than a representation of the hexadecimal number FF in binary:


$x = base_convert ("FF", 16,2); $x = 11111111
$x = base_convert («11111111»,2,16); $x = FF
$x = base_convert («200»,10,16); $x = C8

 


bindec

 

Converts a binary number to a decimal number.
Syntax:

int bindec (string binary_string)


Converts the binary number specified in the string binary_string to a decimal number.

The maximum number that can still be converted is 2,147,483,647


$x = bindec (11111111); $x = 255
$x = bindec (10101010); $x = 170
$x = bindec (2147483647); $x = 1111111111111111111111111111111

decbin

 

Converts a decimal number to a binary number.

Syntax:


string decbin (int $number)


Returns a string that is the binary representation of an integer $number. The maximum number that can still be converted is 2,147,483,647, which looks like 31 units in a binary system.


There are similar functions for the octal and hexadecimal systems. They are called the same, only instead of "bin" are substituted respectively "oct" and "hex".


$x = decbin (255); $x = 11111111
$x = decbin (2147483647); $x = 1111111111111111111111111111111

dechex

Converts a decimal number to a hexadecimal number.


Syntax:


string dechex (int number)


Returns a string that is the hexadecimal representation of an integer number. The maximum number that can still be converted is 2,147,483,647


$x = dechex (2147483647); $x = 7fffffff

decoct

 

onverts a decimal number to an octal number.


Syntax:

string decoct (int number)


Returns a string that is an octal representation of an integer number. The maximum number that can still be converted is 2,147,483,647


$x = dechex (2147483647); $x = 17777777777


hexdec

Converts a hexadecimal number to a decimal number.


Syntax:

int hexdec (string hex_string)


Converts the hexadecimal number specified in the string hex_string to a decimal number. The maximum number that can still be converted is 7fffffff


$x = hexdec (7fffffff); $x = 2147483647


octdec

Converts an octal number to a decimal number.

Syntax:

int octdec (string octal_string)


Converts the octal number specified in the string octal_string to a decimal number. The maximum number that can still be converted is 17777777777


$x = octdec (17777777777); $x = 2147483647


deg2rad

 

Converts degrees to radians.


Syntax:

double deg2rad (double number)


Converts the degrees specified in the number parameter to radians.

rad2deg

Converts radians to degrees.


Syntax:

double rad2deg (double number)


Converts the radians specified in the number parameter to degrees.

number_format

Format a number.


Syntax:

number_format ($number, $decimals, $dec_point=".", $thousands_sep=",");


This function formats a floating-point number by dividing it into triads with the specified precision. It can be invoked with two or four arguments, but not with three! The $decimals parameter specifies how many digits after the decimal point the number in the output string should have.


The $dec_point parameter is an integer and fractional separator, and the $thousands_sep parameter is a triad separator in a number (if you specify an empty string in its place, the triads are not separated from each other).