Re: Re: crc32() and ip2long() return values

From: Date: Fri, 30 Aug 2013 16:27:52 +0000
Subject: Re: Re: crc32() and ip2long() return values
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Fri, Aug 30, 2013 at 10:29 AM, Rasmus Schultz <[email protected]> wrote:
> No replies probably means no one cares. oh well.
>
> For the record, the examples I posted are wrong - the correct way to
> convert the long values consistently appears to be:
>
> list($v) = array_values(unpack('l', pack('l',
> ip2long('255.255.255.0'))));
>
I had spotted the error, but didn't want to reply because I don't
really understand what you are getting at.

The core issue is that PHP doesn't provide a 32-bit unsigned integer
on a 32-bit platform ... and/or that the size of integer changes
depending on the platform. But I doubt that is going to change any
time soon. Crippling 64-bit systems due to old, legacy 32-bit
platforms is shortsighted.

What's wrong with the manual's approach?

$checksum = sprintf("%u", crc32("The quick brown fox jumped over the
lazy dog."));

Are you going to do further mathematical operations on it? You can
take that string and stuff it into an uint32 field into a db without
an issue.

At the end of the day, there's no getting around that PHP programmers
need to be aware of the difference between 32-bit and 64-bit systems
... it affects far more than these two particular functions.

But if these two functions are particularly bothersome, a better "fix"
IMO is just:

$crc = crc32("12341234", CRC32_UINT32_STRING);

Where the second parameter is CRC32_INT (default & current behavior),
CRC32_INT32 (always negative if high bit is set), CRC32_UINT32_STRING,
CRC32_HEX_STRING

Forgive the poor names.

--
Matthew Leverton


Thread (14 messages)

« previous php.internals (#68730) next »