如何计算PHP HEX中的CRC16 CCITT? [英] How to calculate CRC16 CCITT in PHP HEX?

查看:205
本文介绍了如何计算PHP HEX中的CRC16 CCITT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



设备向我发送一个包含校验和的PACKET:


10 00 00 00 00 00 00 00 12 51 09 08 00 18 00 04 02 14 00 0c 00 0c 02
1c 00 02 00 00 00 00 00 00 a0 77


校验和在最后: a0 77



我试过使用

如何在php中计算crc16



将C转换为PHP的CRC16函数

,CRC 16计算返回: E6 F4 而不是 a0 77



当我查找时,我得到正确的十六进制信息:


100000000000000012510908001800040214000c000c021c0002000000000000

p>

http://www.lammertbies.nl/comm/info/crc-calculation.html ,但我无法重现它。 (确保选择输入类型为十六进制)

你能帮我弄清楚如何获得crc16 CCITT字符串的十六进制值


<100> 100000000000000012510908001800040214000c000c021c0002000000000000

我正在寻找校验和 a0 77

解决方案

我能够用下面的实现产生相同的校验和:

  define('CRC16POLYN',0x1021); 

函数CRC16Normal($ buffer){
$ result = 0xFFFF; ($ length = strlen($ buffer))> 0){
for($ offset = 0; $ offset< $ length; $ offset ++){
$ result ^ =(ord($ buffer [$ offset]))(< 8); (($ result <= 1)& 0x10000)$ result ^ = CRC16POLYN;
for($ bitwise = 0; $ bitwise <8; $ bitwise ++)
$ result& = 0xFFFF;
}
}
}
返回$ result;
}

echo dechex(CRC16Normal(hex2bin('100000000000000012510908001800040214000c000c021c0002000000000000')));

以上给出了 a077 p>

https://forums.digitalpoint.com/threads/php-define-function-calculate-crc-16-ccitt.2584389/


I'm trying to use a PHP CRC16 CCITT function to calculate the checksum.

A device sends me a PACKET with Checksum included:

10 00 00 00 00 00 00 00 12 51 09 08 00 18 00 04 02 14 00 0c 00 0c 02 1c 00 02 00 00 00 00 00 00 a0 77

The checksum is at the end: a0 77

I've tried using

How to calculate crc16 in php

Convert C to PHP for CRC16 Function

With no success, the CRC 16 Calculations return: E6 F4 instead of a0 77

I get the correct Hex information returned, when I lookup:

100000000000000012510908001800040214000c000c021c0002000000000000

on the website http://www.lammertbies.nl/comm/info/crc-calculation.html but I cannot reproduce it. (make sure to select the input type to HEX)

Can you please help me figure out how to get the crc16 CCITT of the string of hex values

100000000000000012510908001800040214000c000c021c0002000000000000

I'm looking for the checksum a0 77

解决方案

I was able to produce the same checksum with implementation like below:

define('CRC16POLYN', 0x1021);

function CRC16Normal($buffer) {
    $result = 0xFFFF;
    if (($length = strlen($buffer)) > 0) {
        for ($offset = 0; $offset < $length; $offset++) {
            $result ^= (ord($buffer[$offset]) << 8);
            for ($bitwise = 0; $bitwise < 8; $bitwise++) {
                if (($result <<= 1) & 0x10000) $result ^= CRC16POLYN;
                $result &= 0xFFFF;
            }
        }
    }
    return $result;
}

echo dechex(CRC16Normal(hex2bin('100000000000000012510908001800040214000c000c021c0002000000000000')));

Above gives a077 on output.

Code snippet found on https://forums.digitalpoint.com/threads/php-define-function-calculate-crc-16-ccitt.2584389/

这篇关于如何计算PHP HEX中的CRC16 CCITT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆