Remembering that there I used an example of 8 bits that well spoke the mgibsonbr, is not the way that PHP assembles numbers. This makes all the difference because it will need to do data alignment and will generate a different number than expected because of the signal position.
If you really want to ensure that the first bit of what you send is the signal, as far as I know, you can only do it manually. A documentation have an example:
function bin2si($bin, $bits = 32) {
if (strlen($bin) == $bits) {
if (substr($bin, 0, 1) == 0) { // positive or zero
$si = base_convert($bin, 2, 10);
} else { // negative
$si = base_convert($bin, 2, 10);
$si = -(pow(2, $bits) - $si);
}
return $si;
}
}
echo bin2si('11111101', 8);
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
A curiosity: Why the function name is
bin2si
? What would that besi
?– Wallace Maxters
signed integer
.– Maniero