Hi, is anyone who can help me with my program? which i'm trying to calculate the checksum of the data packet 01 01 01 00 00 that is total of 5 bytes. My program is as below
[CODE=vb]Option Explicit
Dim i As Integer, j As Integer
Dim bStatus As Boolean
Dim cPacket(5) As Byte
Dim nCRC As Long
Private Sub Form_Load()
cPacket(0) = &H1
cPacket(1) = &H1
cPacket(2) = &H1
cPacket(3) = &H0
cPacket(4) = &H0
nCRC = &HFFFF
For i = 0 To Ubound(cPacket)-1
nCRC = nCRC Xor cPacket(i)
For j = 0 To 7
If ((nCRC And &H1) = True) Then
nCRC = nCRC/2
nCRC = nCRC Xor &H8408
Else
nCRC = nCRC/2
End If
Next j
Next i
Debug.Print nCRC
End Sub
[/CODE]
This program is where i was refering to the C++ code i.e.
[CODE=cpp]DWORD i,j;
for(i=0;i<nLen; i++)
{
nCRC^=cPacket[i];
for(j=0;j<8;j++ )
{
if(nCRC & 0x0001)
nCRC=(nCRC>>1) ^ 0x8408;
else
nCRC=(nCRC>>1);
}
}
return nCRC;[/CODE]
If everything is correct i should get the answer like this
data packet:01 01 01 00 00 or (0x01 0x01 0x01 0x00 0x00)
nCRC= 0x7DAB
data packet:01 00 00 00 00 or (0x01 0x00 0x00 0x00 0x00)
nCRC= 0x3BCC
But i dont know where is goes wrong....Hope anyone can help. Thanks!
[CODE=vb]Option Explicit
Dim i As Integer, j As Integer
Dim bStatus As Boolean
Dim cPacket(5) As Byte
Dim nCRC As Long
Private Sub Form_Load()
cPacket(0) = &H1
cPacket(1) = &H1
cPacket(2) = &H1
cPacket(3) = &H0
cPacket(4) = &H0
nCRC = &HFFFF
For i = 0 To Ubound(cPacket)-1
nCRC = nCRC Xor cPacket(i)
For j = 0 To 7
If ((nCRC And &H1) = True) Then
nCRC = nCRC/2
nCRC = nCRC Xor &H8408
Else
nCRC = nCRC/2
End If
Next j
Next i
Debug.Print nCRC
End Sub
[/CODE]
This program is where i was refering to the C++ code i.e.
[CODE=cpp]DWORD i,j;
for(i=0;i<nLen; i++)
{
nCRC^=cPacket[i];
for(j=0;j<8;j++ )
{
if(nCRC & 0x0001)
nCRC=(nCRC>>1) ^ 0x8408;
else
nCRC=(nCRC>>1);
}
}
return nCRC;[/CODE]
If everything is correct i should get the answer like this
data packet:01 01 01 00 00 or (0x01 0x01 0x01 0x00 0x00)
nCRC= 0x7DAB
data packet:01 00 00 00 00 or (0x01 0x00 0x00 0x00 0x00)
nCRC= 0x3BCC
But i dont know where is goes wrong....Hope anyone can help. Thanks!
Comment