Hi.
I would like some help with the conversion from this function from vb.net :
B4X:
Public Function ComputeCRC(val As Byte()) As Byte()
Dim crc As Long
Dim q As Long
Dim c As Byte
crc = 0
For i As Integer = 0 To val.Length - 1
c = val(i)
q = (crc Xor c) And &HF
crc = (crc >> 4) Xor (q * &H1081)
q = (crc Xor (c >> 4)) And &HF
crc = (crc >> 4) Xor (q * &H1081)
Next
Return New Byte() {CByte(crc And &HFF), CByte(crc >> 8)}
End Function
I need this to calculate the checksum for my data that i`m sending to the SerialPort.
I tried making a conversion, but I don`t know even how to begin.
Any help would be appreciated.
Thank you.
It is better to post an example of input and the expected output so we can test it.
Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
Dim crc As Long
Dim q As Long
Dim c As Byte
For i = 0 To val.Length - 1
c = val(i)
q = Bit.And(Bit.Xor(crc, c), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
Next
Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.
It is better to post an example of input and the expected output so we can test it.
Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
Dim crc As Long
Dim q As Long
Dim c As Byte
For i = 0 To val.Length - 1
c = val(i)
q = Bit.And(Bit.Xor(crc, c), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
Next
Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.
Dim FstLine As String="FC0511"
Dim Secline As String= bc.HexFromBytes(ComputeCRC(bc.HexToBytes(FstLine)))
Log(FstLine & Secline)
The result that I expected was : "FC05112756" , and the conversion is working, i have the same result in B4j.
Thank you very much for your help and for this awesome IDE .I`m still learning, but i made some basic programs that i use on my Raspberry Pi , including the usage of the GPIO`s.
Best wishes.
It is better to post an example of input and the expected output so we can test it.
Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
Dim crc As Long
Dim q As Long
Dim c As Byte
For i = 0 To val.Length - 1
c = val(i)
q = Bit.And(Bit.Xor(crc, c), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
Next
Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.