Crc

ckoehn

Banned
How would you translate something like this from vb6 to b4a:

Sub CRCCalculation(nAA As Integer, nLCRC As Integer, nHCRC As Integer)

Dim nBB As Integer
Dim nDD As Integer
Dim nEE As Integer

nBB = nLCRC
nDD = (((nAA Xor nHCRC) * 2) And 254) Xor (nAA Xor nHCRC)
nEE = Int(nDD / 4) Or (nDD And 3) * 64
nDD = nEE Xor nDD
nLCRC = (nEE And 63) Xor (((Int(nDD / 16) Or (nDD Mod 16) * 16) Xor nDD) And 192)
nHCRC = nEE And 192 Xor (Int(nLCRC / 128)) Xor nBB



End Sub

Later,
Clint
 

rbsoft

Active Member
Licensed User
Longtime User
Try this:

B4X:
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.

Rolf
 
Upvote 0

ckoehn

Banned
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

Later,
Clint
 
Upvote 0

jfitchett56

Member
Licensed User
Longtime User
32bit crc on bytearray

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.

Any ideas?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 
Upvote 0

jfitchett56

Member
Licensed User
Longtime User
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. :)
 
Upvote 0

jfitchett56

Member
Licensed User
Longtime User
CRC16 in C++

'ushort ComputeCRC16(const uchar* buf, uint len)
'{
' ushort crc = 0;
' For (uint j = 0; j < len; j++)
' {
' uchar b = buf[j];
' For (uchar i = 0; i < 8; i++)
' {
' crc = ((b ^ (uchar)crc) & 1) ? ((crc >> 1) ^ 0xA001) : (crc >> 1);
' b >>= 1;
' }
' }
' Return crc;
'}
B4X:
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
 
Last edited by a moderator:
Upvote 0

jfitchett56

Member
Licensed User
Longtime User
crc16

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.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
crc16

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?

thanks for your help, hope i make sense.

thanks,
Walter
 
Upvote 0

mrred128

Active Member
Licensed User
Longtime User
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.
 
Upvote 0
Top