Android Question crc computing error

ajk

Active Member
Licensed User
Longtime User
I have found

B4X:
Sub GetCheckSum(MyData As String) As String
    Dim CRC = 255 As Int
    If Not(MyData.StartsWith("02")) Then
        MyData = "02" & MyData
    End If
    Try
        Dim CRC = 255 As Int
        Do While MyData.Length >= 2
            CRC = Bit.Xor(CRC, Bit.ParseInt(MyData.SubString2(0, 2), 16))
            MyData = MyData.SubString(2)
        Loop
        Dim tmp(1) As Byte
        tmp(0) = CRC
        Dim tmpStr = conv.HexFromBytes(tmp) As String
        Return tmpStr
    Catch
        Log("Couldn't convert! Error was: " & LastException.Message)
        Return("00")
    End Try
 
End Sub

from https://www.b4x.com/android/forum/threads/xor-checksum-hell.37364/#post-220250

Post is too old to reply.

Question is still alive - what wrong with this code? It produces result "a bit" to big

I have tested it for "P0#i100/" ($30 $23 $69 $31 $30 $30 $2F) - it gives C9, but should be B9
 
Last edited:

sorex

Expert
Licensed User
Longtime User
is the bold text what you feed to the sub?

I wonder how you can even get to C9 or B9 when the highest value doesn't even touch bit 7 ?
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
it should be 255 & that string: $ FF $30 $23 $69 $31 $30 $30 $2F.

The string in first post is tested in code, 255 is added in code (in original code that I have copy-pasted was 0. I have changed it during test to 255 - according to my needs)
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
I'm getting 9B in B4A & on a C64 so I guess the code is right.

and CB with the additional P added to it.
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
Thank you very much - I have removed
B4X:
'    If Not(MyData.StartsWith("02")) Then
'        MyData = "02" & MyData
'    End If
and now works correctly
 
Upvote 0
Top