Sub crc_calculate(Datas As String)
Dim Buffer() As Byte
Buffer = byte1.HexToBytes(Datas)
Dim crc As Int
Dim crcstring As String
crc = 0x0000FFFF
For i = 0 To Buffer.Length - 1
crc = crc_accumulate(Buffer(i), crc)
Log(crc)
Next
crcstring = HexShift(Bit.ToHexString(crc).ToUpperCase)
Return crcstring
End Sub
Sub crc_accumulate(Data As Byte, crc As Int) As Int
Dim tmp, tmpdata As Int
Dim crcaccum As Int
crcaccum = Bit.AND(crc, 0x000000ff)
tmpdata = Bit.AND(Data, 0x000000ff)
tmp = Bit.Xor(tmpdata, crcaccum)
tmp = Bit.AND(tmp, 0x000000ff)
Dim tmp4 As Int
tmp4 = Bit.ShiftLeft(tmp, 4)
tmp4 = Bit.AND(tmp4, 0x000000ff)
tmp = Bit.Xor(tmp, tmp4)
tmp = Bit.AND(tmp, 0x000000ff)
Dim crch As Int
crch = Bit.ShiftRight(crc, 8)
crch = Bit.AND(crch, 0x0000ffff)
Dim tmp8 As Int
tmp8 = Bit.ShiftLeft(tmp, 8)
tmp8 = Bit.AND(tmp8, 0x0000ffff)
Dim tmp3 As Int
tmp3 = Bit.ShiftLeft(tmp, 3)
tmp3 = Bit.AND(tmp3, 0x0000ffff)
tmp4 = Bit.ShiftRight(tmp, 4)
tmp4 = Bit.AND(tmp4, 0x0000ffff)
Dim tmpa As Int
tmpa = Bit.Xor(crch, tmp8)
tmpa = Bit.AND(tmpa, 0x0000ffff)
Dim tmpb As Int
tmpb = Bit.Xor(tmp3, tmp4)
tmpb = Bit.AND(tmpb, 0x0000ffff)
crc = Bit.Xor(tmpa, tmpb)
crc = Bit.AND(crc, 0x0000ffff)
Return crc
End Sub