len是data[]的下标
#define CRC_POLYNOM 0x8408
#define CRC_PRESET 0xFFFF
unsigned Reader::GetCRC(unsigned char data[], int len)
{
unsigned crc = CRC_PRESET;
int i,j;
for (i = 0; i < len; i++) /* len = number of protocol bytes without CRC */
{
crc ^= data[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc = (crc >> 1);
crc ^= CRC_POLYNOM;
}
else
crc = (crc >> 1);
}
}
return crc;
}
#define CRC_POLYNOM 0x8408
#define CRC_PRESET 0xFFFF
unsigned Reader::GetCRC(unsigned char data[], int len)
{
unsigned crc = CRC_PRESET;
int i,j;
for (i = 0; i < len; i++) /* len = number of protocol bytes without CRC */
{
crc ^= data[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc = (crc >> 1);
crc ^= CRC_POLYNOM;
}
else
crc = (crc >> 1);
}
}
return crc;
}
Const CRC_PRESET = &HFFFF
Private Function GetCRC(data() As Byte, ByVal mlen As Long) As Long
Dim CRC As Long
CRC = CRC_PRESET
Dim i As Long, j As Long
For i = 0 To UBound(data)'For i = 0 To mlen-1 也行
CRC = CRC Xor data(i) 'crc ^= data[i]; 异或
For j = 0 To 7
If (CRC And &H1) Then
CRC = CRC \ 2 'crc = (crc >> 1); 右移一位
CRC = CRC Xor CRC_POLYNOM
Else
CRC = CRC \ 2
End If
Next
Next
GetCRC = CRC
End Function
####################### VC ####################
cSendProt[0] =0x07;
cSendProt[1] =0xFE;
cSendProt[2] =0xB0;
cSendProt[3] =0x2B;
cSendProt[4] =0x00 unsigned crc = GetCRC(cSendProt,5);
CRC = 38116***********************vb******************
'cSendProt(0) = &H7
'cSendProt(1) = &HFE
'cSendProt(2) = &HB0
'cSendProt(3) = &H2B
'cSendProt(4) = &H0
CRC = GetCRC(cSendProt, 5)
CRC = 19458*********************************************8例2:
###################### VC ####################
cSendProt[0] =0x08;
cSendProt[1] =0xFE;
cSendProt[2] =0xB0;
cSendProt[3] =0x29;
cSendProt[4] =0x00;
cSendProt[5] =0x01;
unsigned crc = GetCRC(cSendProt,6);
CRC = 14342
################################################******************** VB ***********************cSendProt(0) = &H8
cSendProt(1) = &HFE
cSendProt(2) = &HB0
cSendProt(3) = &H29
cSendProt(4) = &H0
cSendProt(5) = &H1
CRC = GetCRC(cSendProt, 5)
CRC = 3650
*********************************************
http://www.iland.net/~jhaase/vbcrc
printf("%d", crc);
看结果是不是一样的?
Const CRC_POLYNOM = &H8408&
'#define CRC_PRESET 0xFFFF
Const CRC_PRESET = &HFFFF&
'unsigned Reader::GetCRC(unsigned char data[], int len)
Function GetCRC(bData() As Byte, ByVal lLen As Long) As Long
'{
' unsigned crc = CRC_PRESET;
Dim crc As Long
crc = CRC_PRESET
' int i,j;
Dim i As Long, j As Long
' for (i = 0; i < len; i++) /* len = number of protocol bytes without CRC */
For i = 0 To lLen - 1
' {
' crc ^= data[i];
crc = crc Xor CLng(bData(i))
' for (j = 0; j < 8; j++)
For j = 0 To 7
' {
' if (crc & 0x0001)
If crc And &H1& Then
' {
' crc = (crc >> 1);
crc = crc \ 2
' crc ^= CRC_POLYNOM;
crc = crc Xor CRC_POLYNOM
' }
' Else
Else
' crc = (crc >> 1);
crc = crc \ 2
' }
End If
Next
' }
Next
' return crc;
GetCRC = crc
'}
End FunctionPrivate Sub Form_Load()
Dim cSendProt(5) As Byte
cSendProt(0) = &H8
cSendProt(1) = &HFE
cSendProt(2) = &HB0
cSendProt(3) = &H29
cSendProt(4) = &H0
cSendProt(5) = &H1
Debug.Print GetCRC(cSendProt(), 6)
End Sub
Const CRC_POLYNOM = &H8408&
Const CRC_PRESET = &HFFFF&
比较典型的低级错误:)