Skip to content
Advertisement

How to convert Java CRC32 checksum calculation to PHP code [closed]

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();

Advertisement

Answer

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 = "Hello World!";
        CRC32 crc = new CRC32();
        crc.update(input.getBytes());
        System.out.println("input:"+input);
        System.out.println("CRC32:"+crc.getValue());
    }
}

Output:

input:Hello World!
CRC32:472456355

PHP offers a function for calculating the CRC32 checksum.

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

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

Try self with PHP Sandbox.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement