如何将Java的CRC32校验和计算转换为PHP代码

huangapple go评论77阅读模式
英文:

How to convert Java CRC32 checksum calculation to PHP code

问题

以下是您要的翻译:

$byteArr = str2bin($str);
$checksumObj = hash_init('crc32b');
hash_update($checksumObj, $byteArr);
$checksum = hexdec(hash_final($checksumObj));

请注意,CRC32 校验和计算可能因语言和库的不同而略有差异,因此在实际使用时可能需要进行调整和测试。

英文:

I trying to convert below Java code to PHP. Kindly provide a solution for me.

byte[] byteArr=str.getBytes();<br>
Checksum checksumObj = new CRC32();<br>
checksumObj.update(byteArr, 0, byteArr.length);<br>
Long checksum=checksumObj.getValue();

答案1

得分: 0

用Java代码提供一个完整的可复现示例对这个问题很有帮助

import java.util.zip.CRC32;

// 计算字符串的CRC32校验和
public class CRC32Generator {

    public static void main(String[] args) {
        String input = "Hello World!";
        CRC32 crc = new CRC32();
        crc.update(input.getBytes());
        System.out.println("input:" + input);
        System.out.println("CRC32:" + crc.getValue());
    }
}
PHP提供了一个[用于计算CRC32校验和的函数][1]

<?php
$input = "Hello World!";
$crc32 = crc32($input);

echo 'input:' . $input;
echo " CRC32:" . $crc32;
?>

[在PHP沙箱中尝试][2]。

  [1]: https://www.php.net/manual/de/function.crc32.php
  [2]: http://sandbox.onlinephpfunctions.com/code/b45b8b966085c0dd39942d008318a94ed373711c
英文:

A complete reproducible example with Java code is helpful for such a question.

import java.util.zip.CRC32;
 
// Calculates CRC32 checksum for a string
public class CRC32Generator {
 
    public static void main(String[] args) {
        String input = &quot;Hello World!&quot;;
        CRC32 crc = new CRC32();
        crc.update(input.getBytes());
        System.out.println(&quot;input:&quot;+input);
        System.out.println(&quot;CRC32:&quot;+crc.getValue());
    }
}

Output:

input:Hello World!
CRC32:472456355

PHP offers a function for calculating the CRC32 checksum.

&lt;?php
$input = &quot;Hello World!&quot;;
$crc32 = crc32($input);

echo &#39;input:&#39;.$input;
echo &quot; CRC32:&quot;.$crc32;

Try self with PHP Sandbox.

huangapple
  • 本文由 发表于 2020年8月17日 22:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63453358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定