J'ai besoin de convertir ces fonctions C en C#. Je voulais juste vérifier si je l'ai bien fait. Merci!Fonction CRC. Conversion de C en C#
code C:
unsigned short Crc;
unsigned short update_crc(unsigned short crc, char c) {
char i;
crc ^= (unsigned short)c<<8;
for (i=0; i<8; i++) {
if (crc & 0x8000) crc = (crc<<1)^0x1021;
else crc <<=1;
}
return crc;
}
void exampleCRC(void){
#define INITIAL_CRC 0xffff
unsigned short Crc = INITIAL_CRC;
record_t record;
for (byteCount=0; byteCount<sizeof(record_t); byteCount++) {
Crc = update_crc(Crc, record[byteCount]);
}
}
C# Code:
ushort UpdateCrc(ref ushort crc, byte b)
{
crc ^= (ushort)(b << 8);
for (int i = 0; i < 8; i++)
{
if ((crc & 0x8000) > 0)
crc = (ushort)((crc << 1)^0x1021);
else
crc <<= 1;
}
return crc;
}
ushort CalcCrc(byte[] data)
{
ushort crc = 0xFFFF;
for (int i = 0; i < data.Length; i++)
crc = UpdateCrc(ref crc, data[i]);
return crc;
}
Deux mots: Tests unitaires! – vcsjones
Oui, bonne idée. Merci! – Matt