PHP functions: a detailed explanation of functions for converting between decimal, binary, octal, and hexadecimal.
The main goal is to master methods for converting between number bases so they can be applied in actual development.
Decimal System Conversion Functions
Decimal to Binary: the decbin() Function
Example:
echo decbin(12); // outputs 1100
echo decbin(26); // outputs 11010
decbin (PHP 3, PHP 4, PHP 5)
decbin: converts decimal to binary.
Description:
string decbin(int $number)
Returns a string containing the binary representation of the given number parameter. The maximum value that can be converted is the decimal number 4294967295, whose result is a string of 32 ones.
Decimal to Octal: the decoct() Function
echo decoct(15); // outputs 17
echo decoct(264); // outputs 410
decoct (PHP 3, PHP 4, PHP 5)
decoct: converts decimal to octal.
Description:
string decoct(int $number)
Returns a string containing the octal representation of the given number parameter. The maximum value that can be converted is the decimal number 4294967295, whose result is 37777777777.
Decimal to Hexadecimal: the dechex() Function
echo dechex(10); // outputs a
echo dechex(47); // outputs 2f
dechex (PHP 3, PHP 4, PHP 5)
dechex: converts decimal to hexadecimal.
Description:
string dechex(int $number)
Returns a string containing the hexadecimal representation of the given number parameter. The maximum value that can be converted is the decimal number 4294967295, whose result is ffffffff.
Binary System Conversion Functions
Binary to Decimal: the bindec() Function
echo bindec('110011'); // outputs 51
echo bindec('000110011'); // outputs 51
echo bindec('111'); // outputs 7
bindec (PHP 3, PHP 4, PHP 5)
bindec: converts binary to decimal.
Description:
number bindec(string $binary_string)
Returns the decimal equivalent of the binary number represented by the binary_string parameter.
bindec() converts a binary number to an integer. The largest number it can convert is 31 ones, or 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large values; in that case, it returns a float.
Binary to Hexadecimal: the bin2hex() Function
$binary = '11111001';
$hex = dechex(bindec($binary));
echo $hex; // outputs f9
bin2hex (PHP 3 >= 3.0.9, PHP 4, PHP 5)
bin2hex: converts binary data to hexadecimal representation.
Description:
string bin2hex(string $str)
Returns an ASCII string containing the hexadecimal representation of the str parameter. The conversion is performed byte by byte, with the high nibble first.
Octal System Conversion Functions
Octal to Decimal: the octdec() Function
echo octdec('77'); // outputs 63
echo octdec(decoct(45)); // outputs 45
octdec (PHP 3, PHP 4, PHP 5)
octdec: converts octal to decimal.
Description:
number octdec(string $octal_string)
Returns the decimal equivalent of the octal number represented by the octal_string parameter. The largest value that can be converted is 17777777777, or 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers; in that case, it returns a float.
Hexadecimal Conversion Function Description
Hexadecimal to Decimal: the hexdec() Function
var_dump(hexdec('See'));
var_dump(hexdec('ee'));
// both print "int(238)"
var_dump(hexdec('that')); // prints "int(10)"
var_dump(hexdec('a0')); // prints "int(160)"
hexdec (PHP 3, PHP 4, PHP 5)
hexdec: converts hexadecimal to decimal.
Description:
number hexdec(string $hex_string)
Returns the decimal number equivalent to the hexadecimal number represented by the hex_string parameter. hexdec() converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7fffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers; in that case, it returns a float.
hexdec() replaces all non-hexadecimal characters it encounters with 0. As a result, all leading zeros are ignored, but trailing zeros are included in the value.
Arbitrary Base Conversion: the base_convert() Function
$hexadecimal = 'A37334';
echo base_convert($hexadecimal, 16, 2); // outputs 101000110111001100110100
base_convert (PHP 3 >= 3.0.6, PHP 4, PHP 5)
base_convert: converts a number between arbitrary bases.
Description:
string base_convert(string $number, int $frombase, int $tobase)
Returns a string containing the representation of number in base tobase. The base of number itself is specified by frombase. Both frombase and tobase can only be between 2 and 36, inclusive. Digits greater than decimal are represented with the letters a-z; for example, a represents 10, b represents 11, and z represents 35.
