Calculo CRC 16 X25 em PHP

时间:2019-03-14 18:49:18

标签: checksum crc

我试图找出某些消息的校验和,但我什至无法从此链接https://github.com/meetanthony/crcphp获取github文件。在上面链接的站点中有效,但是当我运行测试时,没有人能够运行CRC16 X25的计算吗?

以下葡萄牙语已被翻译成英语:

Olá,amigos,estou com um issuea,estou tentando descobrir o checksum de determinadas mensagens,masnãoestou conseguindo mesmo com或arquivos do github deste链接https://github.com/meetanthony/crcphp。 没有站点可以链接acima funciona,CRC CRC16 X25和Alguémconseguiu fazer funcionar o calulo para?

1 个答案:

答案 0 :(得分:0)

我发现了为什么我的代码错了, 我以十六进制传递一个数字并进行计算,我需要将其从十六进制转换为十进制,然后使用自适应函数(hexdec($ d)解决了我的问题):

public function ComputeCrc($crcParams, $data) {
    if ($crcParams->RefIn) {
        $crc = $crcParams->InvertedInit;
    } else {
        $crc = $crcParams->Init;
    }
    if ($crcParams->RefOut) {
        foreach ($data as $d) {
            $d = hexdec($d);
            echo "<br>".$d."<br>";
            $crc = $crcParams->Array[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
        }
    } else {
        foreach ($data as $d) {
            $crc = $crcParams->Array[(($crc >> 8) ^ $d) & 0xFF] ^ ($crc << 8);
        }
    }

    $crc = $crc ^ $crcParams->XorOut;

    $result = new CrcResult();
    $result->Crc = $crc & 0xFFFF;

    return $result;
}