I am trying to write a simple CRC calculator in VB6 I am converting a C code(listed below) I need to use a bitwise & operator. I have read that the Imp operator is what I should use, but it does not give the correct results
Here is the code with the Imp operator
Private Sub cmdCalculate_Click()
Dim str As String
Dim strLength As Long, ptrLengthField As Long
Dim ptr As Long
Dim i As Integer, j As Integer, c As Variant, crc As Long
crc = 0
strLength = Len(txtInputString.Text)
str = txtInputString.Text
For i = 1 To strLength
c = Val("&H" & Mid(str, i, 1)) And 255 'convert Hex characters to integers
For j = 0 To 8
If crc And 32768 Then
crc = crc * 2 'bitshift left
crc = crc + (((c * 2) Imp &H100) <> 0) ' bitshift left then bitwise and &H100
crc = crc Xor 4129
Else
crc = crc * 2
crc = crc + (((c * 2) Imp &H100) <> 0)
End If
Next
Next
lblCRC = Hex(crc)
End Sub
This is the C Code I'm trying to convert
//
// CRC Calculation & Checking routines for Kingfisher Protocol
//
unsigned int kf_crc_calc(BYTE *string, int charnum)
{
int i, j;
int c, crc = 0;
for (i=0; i<charnum; i++)
{ c = string & 0xff;
for (j=0; j<8; j++)
{ if (crc & 0x8000)
{ crc <<= 1;
crc += (((c <<= 1) & 0x100) != 0);
crc ^= 0x1021;
}
else
{ crc <<= 1;
crc += (((c <<= 1) & 0x100) != 0);
}
}
}
return(crc);
}