fhem-mirror/fhem/contrib/crc.pl
rudolfkoenig 71fe020f84 Initial version
git-svn-id: https://svn.fhem.de/fhem/trunk@3 2b470e98-0d58-463d-a4d8-8e2adae1ed80
2007-01-30 12:47:36 +00:00

34 lines
951 B
Raku
Executable File

die("Usage: crc HEX-MESSAGE\n") if(int(@ARGV) != 2);
my $msg = $ARGV[0];
$msg =~ s/ //g;
my $des = $ARGV[1];
$des =~ s/ //g;
# FFFF: 77 72 statt 2c 7f
# FFFF: 5C AC statt DC D9
#for(my $ic = 0; $ic < 65536; $ic++) {
for(my $ic = 0; $ic < 2; $ic++) {
my $crc = ($ic == 0?0:0xffffffff);
for(my $i = 0; $i < length($msg); $i += 2) {
my $n = ord(pack('H*', substr($msg, $i, 2)));
my $od = $n;
for my $b (0..7) {
my $crcbit = ($crc & 0x80000000) ? 1 : 0;
my $databit = ($n & 0x80) ? 1 : 0;
$crc <<= 1;
$n <<= 1;
$crc ^= 0x04C11DB7 if($crcbit != $databit);
# printf("%3d.%d %02x CRC %x ($crcbit $databit)\n", $i/2, $b, $n, $crc);
}
# printf("%3d %02x CRC %02x %02x\n", $i/2, $od, ($crc&0xff00)>>8, $crc&0xff);
}
# print "$ic\n" if($ic % 10000 == 0);
printf("%02x %02x\n",($crc&0xff00)>>8,$crc&0xff);
print "got $ic\n"
if(sprintf("%02x%02x",($crc&0xff00)>>8,$crc&0xff) eq $des);
}