John Davidson

Binary to Decimal 32bit signed with PHP


I currently have a Decimal that is coming through as: 4294960896

This is then converted to Binary with this function:


Which equals: 11111111111111111110011100000000

I then need to take that binary and convert it to the 32bit signed version which should be '-6400'

I can't seem to find any built in functions that support 32bit signed output.

My Answer:
Here is a PHP function that can convert a 32-bit signed binary number to its decimal equivalent:

function binaryToSignedDecimal($binary) {
$decimal = bindec($binary);
if ($decimal >= 2147483648) {
$decimal = $decimal - 4294967296;
return $decimal;

$binary = '11111111111111111110011100000000';
$decimal = binaryToSignedDecimal($binary);
echo $decimal; // Output: -6400

This function first converts the binary number to decimal using the `bindec` function. Then, it checks if the decimal number is greater than or equal to 2147483648 (which is the maximum positive value for a 32-bit signed integer). If it is, it subtracts 4294967296 from the decimal number to get the correct negative value.

Rate this post

3 of 5 based on 5111 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us