[英]Why do I get different CRC16 result between C# and VB.NET?
I converted a C# code to VB.NET for CRC16 calculation. 我将C#代码转换为VB.NET以进行CRC16计算。 However, I got different result on C# and VB.NET.
但是,我在C#和VB.NET上得到了不同的结果。 I am confused!
我很迷惑! Can someone please point me to the right direction?
有人可以指出我正确的方向吗?
//C# Codes
UInt16 CalculateCRC(Byte dchar, UInt16 crc16)
{
UInt16 mask = (UInt16) (dchar & 0x00FF);
crc16 = (UInt16)(crc16 ^ mask);
for (int i = 0; i < 8; i++)
{
UInt16 compare = (UInt16)(crc16 & 0x0001);
if ( compare == 1)
{
mask = (UInt16)(crc16 / 2);
crc16 = (UInt16)(mask ^ 0xA001);
}
else
{
mask = (UInt16)(crc16 / 2);
crc16 = mask;
}
}
return crc16;
}
'VB.NET Codes
Private Function CalculateCRC(ByVal dchar As Byte, ByVal crc16 As UInt16) As UInt16
Dim mask As UInt16 = Convert.ToUInt16((dchar And &HFF))
crc16 = Convert.ToUInt16((crc16 Xor mask))
Dim compare As UInt16
For i As Integer = 0 To 7
compare = Convert.ToUInt16((crc16 And &H1))
If compare = 1 Then
mask = Convert.ToUInt16((crc16 / 2))
crc16 = Convert.ToUInt16((mask Xor &HA001))
Else
mask = Convert.ToUInt16((crc16 / 2))
crc16 = mask
End If
Next
Return crc16
End Function
OUTPUT
dchar = 1
Variable values for each iteration (0 to 7)
C#
COMPARE : 0, 1, 0, 1, 0, 1, 0, 1
MASK : 32767, 16383, 20479, 10239, 17407, 8703, 16639, 8319,
CRC16 : 32767, 40958, 20479, 38414, 17407, 33278, 16639, 32894
VB.NET
COMPARE : 0, 1, 1, 1, 1, 1, 1, 1
MASK : 32767, 16384, 28672, 26624, 25600, 25088, 24832, 24704
CRC16 : 32767, 57345, 53249, 51201, 50177, 49665, 49409, 49281
Can somebody please point me where did I do wrong in the VB codes? 有人可以指出我在VB代码中哪里做错了吗? Apparantly the C# codes produced the right result.
显然,C#代码产生了正确的结果。
As Jimi pointed out in a comment, you need to use integer division - also, 'CUShort' should be sufficient: 正如Jimi在评论中指出的那样,你需要使用整数除法 - 同样,'CUShort'应该足够了:
Private Function CalculateCRC(ByVal dchar As Byte, ByVal crc16 As UInt16) As UInt16
Dim mask As UInt16 = CUShort(dchar And &HFF)
crc16 = CUShort(crc16 Xor mask)
For i As Integer = 0 To 7
Dim compare As UInt16 = CUShort(crc16 And &H1)
If compare = 1 Then
mask = CUShort(crc16 \ 2)
crc16 = CUShort(mask Xor &HA001)
Else
mask = CUShort(crc16 \ 2)
crc16 = mask
End If
Next i
Return crc16
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.