In some other languages this problem is a matter of a simple cast. In PHP there's no such concept as all integers have the very same type (signed long long or signed long, architecture-dependent). Yet there are cases where you have to do that. E.g. unpack()
cannot read signed long
little endian byte order, only unsigned.
So, given an imaginary 32bit unsigned integer 4294967295
, how do I get a value for an imaginary signed 32bit integer from it? (That's -1
.)
For example, this can be done with pack
/unpack
, but this seems suboptimal to me.
Is there a better way?
How about 32bit versions of PHP? (They tend to solve this problem "automatically" with an integer overflow, but I rather not to use this architecture-dependent workarounds. Background.)