I’m having a problem with converting binary strings to signed integers
If you call decbin(‘-40’), php will output 1111111111111111111111111111111111111111111111111111111111011000
But if you call bindec(decbin(‘-40’)), it will output 1.84467440737E+19 (or something similar, which is obviously not -40) because it “sees the most significant bit as another order of magnitude rather than as the sign bit” – php manual
Is there a way to convert a binary 64 bit binary string (much like the one output by decbin) string into a signed integer?
From the documentation, you cannot use
bindecbase_convertappears to ignore signing altogether.If you know that your incoming string will always be 64 bit binary and you are not on a 32 bit system, it’s quite easy to write a custom function.
Here’s a quick one I knocked together.