Thread Checksum CRC8 Berechnung
(5 answers)
Opened by Lauvia at 2015-05-27 12:20
angelehnt an http://www.miscel.dk/MiscEl/CRCcalculations.html#C...
Ungetestet! Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ######## Quelle: http://www.miscel.dk/MiscEl/CRCcalculations.html#CRC8 ##### use strict; use warnings; use 5.010; ## CRC8 bit sub Crc8 { my $Buffer = shift; my $Poly = shift; my $Init = shift; my @Buffer = split //,$Buffer; my $Result = $Init; for my $i (1..length($Buffer)) { $Result = $Result ^ ord($Buffer[$i-1]); for my $j (0..7) { $Result = ($Result & 0x80) ? ($Result << 1) ^ $Poly : $Result << 1; } } $Result = $Result & 0xFF; return $Result; } ### CRC8 byte, normal my @CrcTable = (); sub GenerateTableCrc8 { my $Poly = shift; for my $i (0..255) { $CrcTable[$i]= Crc8(chr($i),$Poly,0); } } sub Crc8Byte { my $Buffer = shift; my $Initial = shift; my $Result = $Initial; my @Buffer = split //,$Buffer; for my $i(1..length($Buffer)){ $Result = ($Result << 8) ^ $CrcTable[ (ord($Buffer[$i-1]) ^ ($Result)) & 0xFF ]; } $Result = $Result & 0xFF; return $Result; } Du musst dann wohl so arbeiten: Code (perl): (dl
)
1 2 3 4 GenerateTableCrc8(0x2F); my $daten = 'Dead cows dont make Mooooo!'; my $crc = Crc8Byte($daten,0xFF); print $crc; Editiert von GwenDragon: Tippfehler im Code Last edited: 2015-05-29 18:01:36 +0200 (CEST) |