Android Question String CRC32

KZero

Active Member
Licensed User
Longtime User
Hi,

B4X:
Sub Crc16(Txt As String) As String

    Dim Crc As Int : Crc = 0xFFFF
    Dim bytes() As Byte
    bytes=Txt.GetBytes("UTF8")
    For i = 0 To bytes.Length - 1
         Crc = Bit.Xor(Bit.ShiftLeft(Crc, 8), CrcTable(Bit.Xor(Bit.AND(0xFF,Bit.ShiftRight(Crc, 8)), Bit.AND(0xFF,bytes(i)))))
        Crc = Bit.AND(Crc, 0xFFFF)
    Next
    Return Bit.ToHexString(Crc).ToUpperCase

End Sub

i have this function for Crc16 but i didn't find any slimier function for Crc32

thanks
 

keirS

Well-Known Member
Licensed User
Longtime User
This class uses the Reflection library to call the CRC32 class in the Android API
B4X:
'Class module
Sub Class_Globals
   Dim oReflector As Reflector
   Dim oByteConverter As ByteConverter
   Dim omUpdate As Object
   Dim omReset As Object
   Dim omGetValue As Object
  
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize


  'Create an instance of theCRC32 class using reflection and set it as the target of the Reflector.
  oReflector.Target = oReflector.CreateObject("java.util.zip.CRC32")

  'Register the Update method of the CRC32 class [public void update (byte[] buf)]
  'Adds the byte array passed to the data that the checksum is being calculated for

  'Set the type of the parameter passed for the  update method to Byte Array.
  Dim Types(1) As String
  Types(0) = "[B"
  omUpdate= oReflector.GetMethod("update",Types)
  Types(0) = ""

  'Register the get method of the CRC32 class [public long getValue ()]
  'Returns the CRC32 checksum as a long
  'No parameters for getValue so pass null
  omGetValue =  oReflector.GetMethod("getValue",Null)

'Register the reset method of the CRC32 class [public void reset ()]
'Clears the data that the checksum is being calculated for so a new checksum can be calculated
  omReset = oReflector.GetMethod("reset",Null)
End Sub

Public Sub GetValue As Long
'Return the CRC32 checksum value as a long
'Passes no arguments so use null
    Return  oReflector.InvokeMethod(oReflector.Target,omGetValue,Null)
End Sub

Public Sub Reset
'Clears the buffer so a new checksum can be calculated
'Passes no arguments so use null
    oReflector.InvokeMethod(oReflector.Target,omReset,Null)
   
End Sub

Public Sub GetHexString
'Returns the checksum as a string of Hex values
     Dim aCRCBytes(1) As Byte
    Dim aCRCValue(1) As Long
    aCRCValue(0) = GetValue
    aCRCBytes = oByteConverter.LongsToBytes(aCRCValue)
    Return oByteConverter.HexFromBytes(aCRCBytes)
End Sub

Public Sub Update(strAddToBuffer As String)
'Adds the passed string to the buffer that the checksum is being calculated for
   Dim strBuffer As String
   Dim aBytesToAdd(1) As Byte
   Dim Args(1) As Object
   aBytesToAdd  = oByteConverter.StringToBytes(strAddToBuffer,"UTF8")
   Args(0) = aBytesToAdd
   oReflector.InvokeMethod(oReflector.Target,omUpdate,Args) 
End Sub

The test program that calls the class

B4X:
Sub Process_Globals

   Dim oCRC32 As CRC32Class
  
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim  otxtStrToCheckSum As EditText
   Dim olblChecksum As Label
   Dim obtnGenChecksum As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
   Dim cDateTime As String
   otxtStrToCheckSum .Initialize("")
   obtnGenChecksum.Initialize("obtnGenChecksum")
   olblChecksum.Initialize("")
   obtnGenChecksum.Text =  "Calc CRC32"
   olblChecksum.TextSize = 14
   Activity.AddView(otxtStrToCheckSum,5,5,250,100)
   Activity.AddView(obtnGenChecksum,5,120,100,40)
   Activity.AddView(olblChecksum,5,180,250,40)
  
   'Create CRC32 Object
   oCRC32.Initialize
End Sub


Sub obtnGenChecksum_Click
    'Clear the CRC32 Buffer
   oCRC32.Reset
   'Fill buffer with contents of text box
   oCRC32.Update(otxtStrToCheckSum.Text)
   'Calculate Checksum and populate label with result
   olblChecksum.Text = oCRC32.GetHexString
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

KZero

Active Member
Licensed User
Longtime User
This class uses the Reflection library to call the CRC32 class in the Android API
B4X:
'Class module
Sub Class_Globals
   Dim oReflector As Reflector
   Dim oByteConverter As ByteConverter
   Dim omUpdate As Object
   Dim omReset As Object
   Dim omGetValue As Object


End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize


  'Create an instance of theCRC32 class using reflection and set it as the target of the Reflector.
  oReflector.Target = oReflector.CreateObject("java.util.zip.CRC32")

  'Register the Update method of the CRC32 class [public void update (byte[] buf)]
  'Adds the byte array passed to the data that the checksum is being calculated for

  'Set the type of the parameter passed for the  update method to Byte Array.
  Dim Types(1) As String
  Types(0) = "[B"
  omUpdate= oReflector.GetMethod("update",Types)
  Types(0) = ""

  'Register the get method of the CRC32 class [public long getValue ()]
  'Returns the CRC32 checksum as a long
  'No parameters for getValue so pass null
  omGetValue =  oReflector.GetMethod("getValue",Null)

'Register the reset method of the CRC32 class [public void reset ()]
'Clears the data that the checksum is being calculated for so a new checksum can be calculated
  omReset = oReflector.GetMethod("reset",Null)
End Sub

Public Sub GetValue As Long
'Return the CRC32 checksum value as a long
'Passes no arguments so use null
    Return  oReflector.InvokeMethod(oReflector.Target,omGetValue,Null)
End Sub

Public Sub Reset
'Clears the buffer so a new checksum can be calculated
'Passes no arguments so use null
    oReflector.InvokeMethod(oReflector.Target,omReset,Null)
 
End Sub

Public Sub GetHexString
'Returns the checksum as a string of Hex values
     Dim aCRCBytes(1) As Byte
    Dim aCRCValue(1) As Long
    aCRCValue(0) = GetValue
    aCRCBytes = oByteConverter.LongsToBytes(aCRCValue)
    Return oByteConverter.HexFromBytes(aCRCBytes)
End Sub

Public Sub Update(strAddToBuffer As String)
'Adds the passed string to the buffer that the checksum is being calculated for
   Dim strBuffer As String
   Dim aBytesToAdd(1) As Byte
   Dim Args(1) As Object
   aBytesToAdd  = oByteConverter.StringToBytes(strAddToBuffer,"UTF8")
   Args(0) = aBytesToAdd
   oReflector.InvokeMethod(oReflector.Target,omUpdate,Args)
End Sub

The test program that calls the class

B4X:
Sub Process_Globals

   Dim oCRC32 As CRC32Class

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim  otxtStrToCheckSum As EditText
   Dim olblChecksum As Label
   Dim obtnGenChecksum As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
   Dim cDateTime As String
   otxtStrToCheckSum .Initialize("")
   obtnGenChecksum.Initialize("obtnGenChecksum")
   olblChecksum.Initialize("")
   obtnGenChecksum.Text =  "Calc CRC32"
   olblChecksum.TextSize = 14
   Activity.AddView(otxtStrToCheckSum,5,5,250,100)
   Activity.AddView(obtnGenChecksum,5,120,100,40)
   Activity.AddView(olblChecksum,5,180,250,40)

   'Create CRC32 Object
   oCRC32.Initialize
End Sub


Sub obtnGenChecksum_Click
    'Clear the CRC32 Buffer
   oCRC32.Reset
   'Fill buffer with contents of text box
   oCRC32.Update(otxtStrToCheckSum.Text)
   'Calculate Checksum and populate label with result
   olblChecksum.Text = oCRC32.GetHexString
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

thanks its working but is it possible to add CrcTable to match the result with my Delphi Application that's use this function ? http://www.delphitips.net/2008/01/01/crc32-cyclic-redundency-check/
 
Upvote 0
Top