Sujet: Re: significations des opérateurs \ et *
De: vlepage (l' arobase) aol.com (val)
Groupes: fr.comp.lang.basic
Organisation: http://groups.google.com
Date: 15. Mar 2008, 11:45:35
On 14 mar, 20:19, "Jean-marc" <NO_SPAM_jean_marc...@yahoo.fr.invalid>
wrote:
val wrote:
Bonjour,
Je transcris un programme vb en c mais je ne sais pas comment
interpréter les opérateurs
\ (anti slash) et * (étoile) dans le code suivant d'un calcul de CRC.
tCRC = 0
For x = 1 To Len(sData)
tIndex = ((tCRC \ &H100&) And &HFF&) Xor Asc(Mid(sData, x, 1))
tCRCa = ((tCRC And &HFF&) * &H100&)
tCRCb = tTable(tIndex) And &HFFFF&
tCRC = (tCRCa Xor tCRCb) And &HFFFF&
Next
CRC16 = tCRC And &HFFFF&
Du coup la traduction devient triviale :
// Il suffit de mettre les bonnes constantes dans tTable ...
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
long tTable[256] = { 0x00, 0x01, 0x02, 0x04,
0x05, 0x06, 0x07, 0x08
/* define constants here */
};
long CRC16(unsigned char *sData)
{
long tCRC = 0L;
long tIndex = 0L;
long tCRCa = 0L;
long tCRCb = 0L;
long size = 0L;
long i = 0L;
size = strlen(sData);
for(i = 0L; i<size; i++)
{
tIndex = ((tCRC >> 8) & 0xFF) ^ sData[i];
tCRCa = (tCRC & 0xFF) << 8;
tCRCb = tTable[tIndex] & 0xFFFF;
tCRC = (tCRCa ^ tCRCb) & 0xFFFF;
}
return tCRC & 0xFFFF;
}
int main(void)
{
unsigned char testString[] = "COUCOU LES GARS";
long crc = 0L;
crc = CRC16(testString);
printf("crc = %ld\n", crc);
return 0;
}
--
Jean-marc Noury (jean_marc_n2)
Microsoft MVP - Visual Basic
FAQ VB:http://faq.vb.free.fr/
mailto: remove '_no_spam_' ; _no_spam_jean_marc...@yahoo.fr- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
Super ! Plus qu'à rajouter
unsigned char hextab[] = "0123456789ABCDEF";
putchar(hextab[(tCRC >> 4) & 0x0F]);
putchar(hextab[tCRC & 0x0F]);
putchar(hextab[(tCRC >> 12) & 0x0F]);
putchar(hextab[(tCRC >> 8) & 0x0F]);
pour avoir les deux octets du CRC en hexa.
Merci !
val