Hallo,
ich habe zur Zeit einen CRC16 Algorithmus der wie folgt aussieht:
Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
| //****************************************************************************** //* CrcByte (CRC berechnen) //****************************************************************************** void CrcByte(unsigned char byte) { // fast bit by bit algorithm without augmented zero bytes. // does not use lookup table, suited for polynom orders between 1...32.
unsigned int c, j, bit;
c = (unsigned int) byte; for (j = 0x0001; j < 0x0100; j <<= 1) { bit = Crc & 0x8000; Crc <<= 1; if (c & j) bit ^= 0x8000; if (bit) Crc ^= CRCPOLY; } } |
diesen möchte ich gerne in C# haben, das habe ich jetzt so umgesetzt:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
| void CrcByte( byte b ) { ushort c, j, bit; c = (ushort)b;
for( j = 0x0001; j < 0x0100; j <<= 1 ) { bit = (ushort)( Crc & 0x8000 ); Crc <<= 1; bool bcj = Convert.ToBoolean( ( c & j ) ); if( bcj ) bit ^= 0x8000; bool bbit = Convert.ToBoolean( bit ); if( bbit ) Crc ^= CRCPOLY; } } |
Das Problem ist, der C# Algo gibt falsche Werte zurück... Kann jemand den Fehler finden, ich steh auf´m Schlauch
Crc ist vom Typ ushort und global definiert, CRCPOLY ist ebenfalls ushort (const) und 0x1021
Gebe ich 0x01 ein müsste als Ergebnis 0x1189 herauskommen, tut es im C Programm auch, im C# Programm allerdings nicht... dort erhalte ich 0x9188
Moderiert von
Christian S.: Topic aus Algorithmen, Optimierung und Assembler verschoben am Mi 27.02.2008 um 14:59