Categories
PHP

Converting numbers between decimal, binary, octal, and hexadecimal

PHP provides the following functions that convert numbers between integer decimal and the commonly used number systems, binary, octal, and hexadecimal.

  1. Converting decimal to binary
  2. Converting binary to decimal
  3. Converting decimal to hexadecimal
  4. Convert hexadecimal to decimal
  5. Converting decimal to octal
  6. Converting octal to decimal
  7. base_​convert() – convert a number between arbitrary bases

Converting decimal to binary

Using decbin() function:

<?php
 $dec = 5;
 echo decbin($dec); # 101

Using base_convert() to covert decimal to binary:

<?php
 $dec = 5;
 echo base_convert($dec, 10, 2);
 #Prints: 101

Using sprintf() function to convert decimal to binary, the %b argument is presented as a binary number:

<?php
 $dec = 5;
 $bin = sprintf('%b', $dec);
 echo $bin; # 101

Converting binary to decimal

Using bindec() function:

<?php
 $bin = 101;
 echo bindec($bin); # 5

Using base_convert() to convert binary to decimal:

<?php
 $bin = 101;
 $dec = base_convert($bin, 2, 10);
 echo $dec; # Prints: 5

Using sprintf() function, the %d argument is presented as a decimal number:

<?php
 $bin = 0b101;
 $dec = sprintf('%d', $bin);
 echo $dec; # Prints: 5

Note: You must prefix the binary number (101) with “0b” (0b101), or the sprintf() function treats it as a decimal number.

Converting decimal to hexadecimal

Using dechex() function:

<?php
 $dec = 1000;
 echo dechex($dec); # 3e8

Using base_convert() function to convert decimal to hexadecimal:

<?php
 $dec = 1000;
 $hex = base_convert($dec, 10, 16);
 echo $hex; # Prints: 3e8

Using sprint() function, the argument %x is presented as a hexadecimal number.

<?php
 $dec = 1000;
 $hex = sprintf('%x', $dec);
 echo $hex; # Prints: 3e8

Converting hexadecimal to decimal

Using hexdec() function:

<?php
 $hex = '3e8';
 echo hexdec($hex); # 1000

Using base_convert() function to convert hexadecimal to decimal:

<?php
 $hex = '3e8';
 $dec = base_convert ($hex, 16, 10);
 echo $dec; # Prints: 1000

Using sprintf() function, the argument %d presented as a decimal number:

<?php
 $hex = 0x3e8;
 $dec = sprintf('%d', $hex);
 echo $dec; # Prints: 1000

Note: You must prefix the hexadecimal number (3e8) with “0x” (0x3e8), or the sprintf() function won’t translate it properly.

Converting decimal to octal

Using decoct() function:

<?php
 $dec = 1000;
 echo decoct($dec); # 1750

Using base_convert() function to convert decimal to octal:

<?php
 $dec = 1000;
 $oct = base_convert($dec, 10, 8);
 echo $oct; # Prints: 1750

Using sprint() function, the argument %o is presented as an octal number.

<?php
 $dec = 1000;
 $oct = sprintf('%o', $dec);
 echo $oct; # 1750

Converting octal to decimal

Using octdec() function:

<?php
 $oct = 1750;
 echo octdec($oct); # 1000

Using base_convert() function to convert octal to decimal:

<?php
 $oct = 1750;
 $dec = base_convert($oct, 8, 10);
 echo $dec; # Prints: 1000

Using sprint() function, the argument %d is presented as a decimal number:

<?php
 $oct = 01750;
 $dec = sprintf('%d', $oct);
 echo $dec; # Prints: 1000

Note: the octal number must be prefixed by 0 (zero) or 0o (as of PHP 8.1), the octal number 1750 can be declared as 01750 or 0o1750.

The base_​convert() function

<?php
 //Syntax
 base_convert(string $num, int $from_base, int $to_base): string

This function takes three parameters:

  1. $num: The number to convert.
  2. $from_base: The current base of the number
  3. $to_base: The base to convert number to

The base_convert() function works for all bases from 2 to 36:

<?php
 $dec = 1000;
 $bin = base_convert($dec, 10, 2);
 echo "Decimal $dec to binary $bin <br>";

 $hex = base_convert($bin, 2, 16);
 echo "Binary $bin to hexadecimal $hex <br>";

 $oct = base_convert($hex, 16, 8);
 echo "Hexadecimal $hex to octel $oct <br>";

 $dec = base_convert($oct, 8, 10);
 echo "Octel $oct to decimal $dec <br>";
 
 # The following output prints on the web browser
/* Decimal 1000 to binary 1111101000
   Binary 1111101000 to hexadecimal 3e8
   Hexadecimal 3e8 to octel 1750
   Octel 1750 to decimal 1000  */

Doing Math:

  1. Finding absolute value, lowest value, and highest value
  2. Using Ceil, Floor, and Round functions
  3. Generating random numbers
  4. Converting numbers between decimal, binary, octal, and hexadecimal
  5. Trigonometry, Exponential and Logarithmic Functions