PHP crc32 inconsistent return values across servers

I’ve been beavering away recently on WishForThis and encountered a nice little issue, worthy of a quick blog post. I use crc32 in PHP for some hashing, which is all fine and dandy for my purposes, but when my site moved to a new server the values returned were inconsistent with my previous server!

This caused quite a headache, but after a bit of investigation it turns out the issue is that my new server is 64-bit whereas the previous was 32-bit. It seems that on 64-bit implementations, crc32 will always return an unsigned integer, whereas on previous versions it could be signed.

The solution? A handy bit of PHP to make the return signed:

function StdCRC32( $uValue )
	$uCRC = crc32( $uValue );
	if( $uCRC & 0x80000000 )
		$uCRC ^= 0xffffffff;
		$uCRC += 1;
		$uCRC = -$uCRC;

	return $uCRC;
Posted in Coding, Web | Tagged | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

> Talk to me!

Twitter profile for Richard Turnbullwitter:
  • Attempting to load Twitter feed..
Facebook profile for Richard Turnbull LinkedIn profile for Richard Turnbull

> Tools

Share |

> Advert