How to convert PHP's crc32 hash into the MySQL equivalent?

后端 未结 2 1649
一整个雨季
一整个雨季 2021-02-15 18:35

Apparently MySQL\'s CRC32() function returns an unsigned BIGINT, while PHP returns hexadecimal value.

In PHP:

hash(\'crc32\',\'hello world\') == 7813f744

相关标签:
2条回答
  • 2021-02-15 19:21

    If you have 64-bit platform you can safely use crc32 function in PHP and CRC32 in MySQL. Quick test:

      php > echo crc32('foobar') . "\n";
      2666930069
    

    MySQL:

      >select crc32('foobar');
      +-----------------+
      | crc32('foobar') |
      +-----------------+
      |      2666930069 |
      +-----------------+
      1 row in set (0.00 sec)
    
    0 讨论(0)
  • 2021-02-15 19:23

    It is possible to use the wrapper function that returns the same value as in mysql:

    function mysql_compatible_crc32($s) {
        $r = crc32($s);
        if($r<0) {
            return 4294967296+$r;
        }
        return $r;
    }
    
    0 讨论(0)
提交回复
热议问题