Sub CRCCalculation(nAA As Int, nLCRC As Int, nHCRC As Int)
Dim nBB As Int
Dim nDD As Int
Dim nEE As Int
nBB = nLCRC
nDD = Bit.Xor(Bit.And((Bit.Xor(nAA, nHCCRC) * 2), 254)), Bit.Xor(nAA, nHCRC))
nEE = Bit.Or(nDD/4, Bit.And(nDD,3) * 64
nDD = Bit.Or(nEE,nDD)
nLCRC = Bit.And(Bit.Xor(Bit.And(nEE,63),Bit.Or(nDD/16,Bit.Xor((nDD Mod 16) * 16),16),192))
nHCRC = Bit.And(nEE, Bit.Xor(192,Bit.Xor((nLCRC/128),nBB)))
End Sub
I do not guarantee that I didn't get the code confused, my brain seems not to functional this morning, seems the brackets put too much strain on it. So adjustment might be needed. But this should give you a start where to go.
This is what I had to change it to to work. I had to place the nLCRC and nHCRC into process globals since there wasn't any ByRef in Android.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim nLCRC As Long
Dim nHCRC As Long
End Sub
Sub CRCCalculation(nAA As Byte)
Dim nBB As Int
Dim nDD As Int
Dim nEE As Int
nBB = nLCRC
nDD = Bit.Xor(Bit.And((Bit.Xor(nAA, nHCRC) * 2), 254), Bit.Xor(nAA, nHCRC))
nEE = Bit.Or((nDD / 4), (Bit.And(nDD, 3) * 64))
nDD = Bit.Xor(nEE, nDD)
nLCRC = Bit.Xor(Bit.And(nEE, 63), Bit.And(Bit.Xor(Bit.Or((nDD / 16), (nDD Mod 16) * 16), nDD), 192))
nHCRC = Bit.Xor(Bit.Xor(Bit.And(nEE, 192), (nLCRC / 128)), nBB)
End Sub
First, this is the absolute best tool for android dev I have seen and I have seen a lot. thank you.
having said that. I need a function to calcuate a 32bit crc from a byte array or from a list. Cant seem to find one. I notices one comes in the java lib but not sure how to use that if it is possible.
You can use this code (requires Reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)
Log(CalcCrc("sdfsdfsdfsdf".GetBytes("UTF8")))
End Sub
Sub CalcCrc(Data() As Byte) As Long
Dim r As Reflector
r.Target = r.CreateObject("java.util.zip.CRC32")
r.RunMethod4("update", Array As Object(Data), Array As String("[B"))
Return r.RunMethod("getValue")
End Sub
Thank you Erel for the insight on the runmethod and java.util.zip. Is there a 16 bit version? turns out the incomming CRC is 16bit based not 32... Found that out thanks to your stellar code.
Sub ComputeCRC16(buf() As Byte) As Int
Dim crc As Int : crc = 0
Dim j As Int
Dim b As Int
Dim i As Int
Dim len As Int
len = buf.Length
For j = 0 To len-1
b = buf(j)
For i = 0 To 7
If crc = Bit.And(Bit.Xor(b,crc),1) Then
Bit.Xor(Bit.ShiftRight(crc,1),0xA001)
Else
Bit.ShiftRight(crc, 1)
End If
b = Bit.ShiftRight(b, 1)
Next
Next
Return crc
End Sub
Hi Erel, Just wanted to let you know i got it working. Thanks for all the help. Once i verify it a little more I will upload the source here somewhere for others who may need it. Thanks again.
Hi Erel, Just wanted to let you know i got it working. Thanks for all the help. Once i verify it a little more I will upload the source here somewhere for others who may need it. Thanks again.
Hi jfitchett56, i was wondering if you could share the code you used to get this working, i need to implement crc check in a project i'm working on, it consists of a bluetooth module, it needs to transmit the values from a microcontroller, the voltage values to be more specific, i need a way to make sure that the data being sent is not corrupted, so i thought of implementing a crc check, could you please share your code, and also since i'm not too familiar with this, how would i convert the values back to normal once i receive them on my phone, or how would I check if the data is corrupted or not?
One thing overlooked is the cumulative calculating of the CRC. In the 'old days' we had to block everything and these routines here assume the entire data is in memory.