能不能把c#语言CRC校验翻成VB
算法(c#语言):
public virtual byte GetCRCByte(byte[] data)
{
int crc = 0x0; (初始值为0)
foreach (byte bt in data)
{
crc = crc ^ bt;
for (int j = 1; j <= 8; j++)
{
if ((crc & 0x80) == 0x80)
crc = (crc << 1) ^ 0xE5;
多项式值为E5,被校验值左移
else
crc = crc << 1; }
}
return (byte)crc;
}
校验示例:
被校验字节串:3165430C00408F01,CRC 值为07H
下面是我翻的,但计算结果不对,不知错在哪里
Private Function crc_chk_value(ByRef data_value() As Integer, ByVal length As Integer)
Dim crc_value As Long
crc_value = &H0
Dim i As Byte
Dim cnt As Integer
For cnt = 0 To length - 1
crc_value = crc_value Xor data_value(cnt)
For i = 0 To 7
If (crc_value And &H80) Then
crc_value = ((crc_value * 2) Mod &HFF) Xor &HE5
Else
crc_value = ((crc_value * 2) Mod &HFF)
End If
Next i
Next cnt
crc_chk_value = crc_value
End Function
[解决办法]
楼主的“翻译”好象不对吧。
试下这段代码:
Private Function GetCRCByte(aData() As Byte) As Byte
Dim CRC As Long
Dim i&, j&
CRC = 0 ' (初始值为0)
For i = LBound(aData) To UBound(aData)
CRC = (CRC Xor aData(i)) And 255
For j = 1 To 8
If (CRC And &H80) Then
CRC = CRC * 2 Xor &HE5
'多项式值为E5 , 被校验值左移
Else
CRC = CRC * 2
End If
Next
Next
GetCRCByte = CRC And 255
End Function