'''uint8 LRC(const uint8* str, const uint16 length)
Sub LRC(A() As Byte, L As Int) As Byte
'''uint8 r=0x00;
Dim R As Byte = 0
'''uint16 i=length;
'''while (i--) {
For I = 0 to L - 1
'''r^=*str++;
R = Bit.Xor(R, A(I))
Next
'''return r;
Return R
End Sub
and your first major confusion will be that B4A Bytes are Java Bytes are signed (-128 to 127) rather than unsigned (0 to 255)
and the second tidy-up is that you can use A.Length to get the side of A() ie no need for the second parameter
and what's kinda funny is that the B4A compiler is going to convert our code straight back to a curly-braces language
perhaps it is better to start from the Java code that is present on Wikipedia rather than from the other code precisely to avoid this difference in types
Hello, I have to calculate the CRC16 of a MODBUS string. Unfortunately I have no idea how can I do that. I have found a code for Visual Basic, how I can convert it for Basic4Android? Public Function CRC(buf() As Byte, lbuf As Integer) As Integer...
What I meant was to translate into B4X starting from Java and thus there will be no type compatibility problems.
The algorithm is described on Wikipedia, in the version that was posted (I don't know if it's swift or MatLab or something else) it seems that it lacks to add the value 1 at the end of the calculation.