Checksum CRC Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alvinlim
    New Member
    • Mar 2008
    • 1

    Checksum CRC Problem

    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!
    Last edited by Killer42; Mar 19 '08, 03:25 AM. Reason: Added CODE=vb and CODE=cpp tags, removed [INDENT]s and fixed indenting
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    What is the actual problem? You just showed us the code and said what it should produce. Should we assume it isn't?

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      At first glance, I can see one problem in the code. I'd suggest you remove the "= True" test.

      When you do a boolean AND you could be producing a value anywhere between &H00 and &HFF. An IF test will take any non-zero value to indicate that it should take the "yes" branch, which is obviously what you intended. But rather than allowing VB to do this, you are explicitly comparing it to the value represented by the named constant True. If I remember correctly, this value is -1 (in hex, probably &HFF or &HFFFF).

      So the first thing I would do is change
      If ((nCRC And &H1) = True) Then
      to
      If (nCRC And &H1) Then

      You could also get away with this, if you really wanted to...
      If ((nCRC And &H1) <> False) Then

      Comment

      Working...