Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • crc32

    Posted on September 17th, 2009 RubyLove 1 comment

    The crc32() function in PHP generates the cyclic redundancy checksum (CRC) polynomial of 32-bit lengths of a string, and returns it as an integer.

    PHP

    $checksum = crc32('hello world');
    echo $checksum;
    // => 222957957

    Ruby

    require 'zlib';
    puts Zlib.crc32('hello world');
    # => 222957957
     

    One response to “crc32”

    1. Take care when comparing ruby results with php results. The php manual says:

      Because PHP’s integer type is signed, and many crc32 checksums will result in negative integers, you need to use the “%u” formatter of sprintf() or printf() to get the string representation of the unsigned crc32 checksum.

      So in case you have a negative number in PHP don’t be afraid, it’s not wrong! Display it with printf(”%u”,$checksum) and compare THAT result o the Ruby result !

    Leave a reply