Reputation: 55
I have the c# code below
public string CRCCalculator(string dtc)
{
ushort crc = 0x0000;
byte[] data = GetBytesFromHexString(dtc);
for (var pos = 0; pos < data.Length; pos++)
{
crc ^= data[pos];
for (var i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
return crc.ToString("X4");
}
But I need this in vb.net, I have personally tried converting it and I have converted plenty of c# code to vb.net in the past however I have not worked with ushort's and bits and such.
Here is the converted VB.net code:
Public Shared Function CRCCalculator(dtc As String) As String
Dim crc As UShort = &H0
Dim data As Byte() = getBytesFromHexString(dtc)
Dim pos = 0
Do While (pos < data.Length)
crc ^= data(pos)
Dim i = 8
Do While (i <> 0)
i = (i - 1)
If (crc And &H1) <> 0 Then
crc >>= 1
crc ^= &HA001
Else
crc >>= 1
End If
Loop
pos = (pos + 1)
Loop
Return crc.ToString("X4")
End Function
Instead of getting a value back I always get 0.
Upvotes: 1
Views: 2284
Reputation: 112209
I don't think "^" does what you think it does. Use "Xor" instead.
C# ^ operator reference - exclusive-or operator
VB ^ operator reference - power-of/exponent operator
Upvotes: 4