PHP: How to convert integer to binary and vice versa

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

Whether you’re dealing with bitwise operations or simply need to display integers in their binary form, PHP has got you covered. Converting between integer and binary is a common task in computer science, and this tutorial aims to provide a solid understanding of how to perform these conversions in PHP.

Understanding the conversion between integer and binary representations is essential for various programming tasks, especially those dealing with low-level data processing. PHP provides built-in functions for these conversions, and this tutorial will guide you through them with practical examples.

Converting Integers to Binary

PHP makes it simple to convert an integer to binary with the built-in decbin() function. Here’s the basic usage:

$integerValue = 42;
$binaryString = decbin($integerValue);
echo $binaryString; // Output: 101010

You can also convert larger numbers:

$integerValue = 1024;
$binaryString = decbin($integerValue);
echo $binaryString; // Output: 10000000000

Converting Binary to Integer

To convert a binary string back to an integer, PHP provides the bindec() function:

$binaryString = '101010';
$integerValue = bindec($binaryString);
echo $integerValue; // Output: 42

This function can handle binary strings of arbitrary length:

$binaryString = '10000000000';
$integerValue = bindec($binaryString);
echo $integerValue; // Output: 1024

Advanced Usage and Precautions

You may encounter situations where you need to work with binary strings that begin with a zero. PHP will ignore leading zeros in strings, so you need to handle this case appropriately:

$binaryString = '00101010';
$integerValue = bindec($binaryString);
echo $integerValue; // Output: 42

Note also that PHP can manage integers up to a certain size, defined by PHP_INT_MAX, which depends on the platform. For 32-bit systems, it typically has a value of 231 – 1, whereas, for 64-bit systems, it’s commonly 263 – 1.

Conversion of Large Numbers and Working with Bitwise Operations

For larger numbers beyond PHP_INT_MAX, or when working with bitwise operations, you can employ the gmp extension for arbitrary precision integers:

$largeNumber = gmp_init('123456789123456789', 10);
$binaryString = gmp_strval($largeNumber, 2);
echo $binaryString; // Output: Binary representation of the large number

To convert large binary strings back to decimal:

$binaryString = '110111100110101011101111101110101101111000001101';
$largeNumber = gmp_strval(gmp_init($binaryString, 2), 10);
echo $largeNumber; // Output: Decimal representation of the large binary string

Working with Floating-Point Numbers

Conversion between binary and integers is straightforward, but what about floating-point numbers? PHP doesn’t provide a direct method for binary conversion of floats. However, you can work around this by using type casting and unpacking:

$float = 42.5;
$binarydata = pack('f', $float); // pack float into binary string
$unpackArr = unpack('f', $binarydata); // unpack binary string into float
$newFloat = $unpackArr[1];
echo $newFloat; // Output: 42.5

Conclusion

Through this tutorial, you should now be comfortable converting integers to binary strings and vice versa in PHP. You’ve also learned how to work with larger numbers and some special cases. Remember that understanding the basis of data representation is key in tackling more advanced programming challenges and can be immensely rewarding in your coding journey.