Android Question CRC8 calculation from string

loccutus

New Member
Licensed User
Longtime User
Hi,

I have to connect to device based on 8 bit AVR Atmel uP, and using CRC8 algorithm in comunication protocol. The Function to calculater CRC8 written in VB.Net is working well but i can't translate it to B4A :( - I will appreciate any help...

B4X:
Function Docrc8(s As String) As Byte

Dim j As Byte
Dim k As Byte
Dim crc8 As Byte
crc8 = 0

For m = 1 To Len(s)
x = Asc(Mid(s, m, 1))
For k = 0 To 7
j = 1 And (x Xor crc8)
crc8 = Fix(crc8 / 2) And &HFF
x = Fix(x / 2) And &HFF
If j <> 0 Then
crc8 = crc8 Xor &H8C
End If
Next k
Next
Docrc8 = crc8
End Function

and my code in B4A - it is not working im getting wrong CRC with "-" sign

B4X:
Sub docrc8(s As String) As Byte
  Dim j As Byte
  Dim k As Byte
  Dim crc8 As Byte = 0
  Dim m As Byte
  Dim x As Int
          
       For m = 1 To sf.Len(s)
         x = Asc(sf.Mid(s, m, 1))
         For k = 0 To 7
           j = Bit.AND(1, Bit.Xor(x, crc8))
           crc8 = Bit.AND(NumberFormat2((crc8/2),2,0,0,False),0xFF)
             x = Bit.AND(NumberFormat2((x/2),2,0,0,False),0xFF)

           If j <> 0 Then
           crc8 = Bit.Xor(crc8 ,0x8C)
           End If
         Next
       Next
     Return crc8
  
   End Sub

what Iam doing wrong ?
 

loccutus

New Member
Licensed User
Longtime User
Here it is... each string begins with ":" sign

:16700910000000000000000 and CRC8 14
:34800950000000000000000 and CRC8 3
:64900950000000000000000 and CRC8 1
:05900910000000000000000 and CRC8 109
:83000950000000000000000 and CRC8 137
:95500950000000000000000 and CRC8 43
:00900910000000000000000 and CRC8 148
:85101010800000000000000 and CRC8 98
:79601010810000000000000 and CRC8 121
:95601010820000000000000 and CRC8 12
:47601010830000000000000 and CRC8 11
:71600920800000000000000 and CRC8 120
:98400920810000000000000 and CRC8 97
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub DoCrc8 (s As String) As Byte
   Dim j, k, crc8 As Int
   Dim x As Int
   Dim fix As Int
   For m = 0 To s.Length - 1
     x = Asc(s.CharAt(m))
     For k = 0 To 7
       j = Bit.AND(1, Bit.Xor(x, crc8))
       fix = crc8 / 2
       crc8 = Bit.AND(fix, 0xff)
       fix = x / 2
       x = Bit.AND(fix, 0xff)
       If j <> 0 Then
         crc8 = Bit.Xor(crc8, 0x8c)
       End If
     Next
   Next
   Return crc8
End Sub
 
Upvote 0

loccutus

New Member
Licensed User
Longtime User
Still not working :( - Im getting "-100" when I should get "156" according to VB.Net code..

Vb.net result :33600950000000000000000 and CRC8 156

B4A result :33600950000000000000000 and CRC8 -100
 
Upvote 0

loccutus

New Member
Licensed User
Longtime User
Like this way ?

B4X:
Sub DoCrc8 (s As String) As Int
  Dim j, k, crc8 As Int
  Dim x As Int
  Dim fix As Int
  For m = 0 To s.Length - 1
    x = Asc(s.CharAt(m))
    For k = 0 To 7
      j = Bit.AND(1, Bit.Xor(x, crc8))
      fix = crc8 / 2
      crc8 = Bit.AND(fix, 0xff)
      fix = x / 2
      x = Bit.AND(fix, 0xff)
      If j <> 0 Then
        crc8 = Bit.Xor(crc8, 0x8c)
      End If
    Next
  Next
  Return crc8
End Sub

no changes.....

4eOVJe9.png


:(

EDIT**************

Everything is ok now :) i had to change to Int type one variable more....

DGC62M7.png


Thank you :)
 
Last edited:
Upvote 0

Similar Threads

Top