Android Question X25CRC Checksum

erickwesz

Member
Licensed User
Longtime User
Hi Guys

My first post in this forum, how can i convert this phyton code to b4a?
I'm trying to do the checksum base on this...

B4X:
class x25crc(object):
    '''x25 CRC - based on checksum.h from mavlink library'''
    def __init__(self, buf=''):
        self.crc = 0xffff
        self.accumulate(buf)

    def accumulate(self, buf):
        '''add in some more bytes'''
        bytes = array.array('B')
        if isinstance(buf, array.array):
            bytes.extend(buf)
        else:
            bytes.fromstring(buf)
        accum = self.crc
        for b in bytes:
            tmp = b ^ (accum & 0xff)
            tmp = (tmp ^ (tmp<<4)) & 0xFF
            accum = (accum>>8) ^ (tmp<<8) ^ (tmp<<3) ^ (tmp>>4)
            accum = accum & 0xFFFF
        self.crc = accum

Here is the perl code i found..
B4X:
#uses MAVLINK's x25 checksum
#https://github.com/mavlink/pymavlink/blob/master/mavutil.py
Sub do_crc{
  my $bufstr= shift; my $len= shift;
  my @buf= unpack( "C".$len, $bufstr );
  #foreach my $b (@buf){ TextOut(UCharToHexstr($b)); } TextOut("!");
  my $crc= 0xFFFF;
  foreach my $b (@buf){
     my $tmp= $b ^ ($crc & 0xFF );
     $tmp= ($tmp ^ ($tmp<<4)) & 0xFF;
     $crc= ($crc>>8) ^ ($tmp<<8) ^ ($tmp<<3) ^ ($tmp>>4);
     $crc= $crc & 0xFFFF;
  }
  #TextOut( " CRC:0x".UIntToHexstr($crc)."!" );
  Return $crc;
}

Thanks for your help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should post an example so it is easier to test it.

Try:
B4X:
Sub X25CRC(data() As Byte) As Int
   Dim crc As Int = 0xffff
   For Each b As Byte In data
     Dim tmp As Int = Bit.Xor(b, Bit.AND(crc, 0xff))
     tmp = Bit.AND(Bit.Xor(tmp, Bit.ShiftLeft(tmp, 4)), 0xff)
     crc = Bit.Xor(Bit.Xor(Bit.Xor(Bit.ShiftRight(crc, 8), Bit.ShiftLeft(tmp, 8)), _
       Bit.ShiftLeft(tmp, 3)), Bit.ShiftRight(tmp, 4))
     crc = Bit.AND(crc, 0xffff)
   Next
   Return crc
End Sub
 
Upvote 0

erickwesz

Member
Licensed User
Longtime User
Hi Erel

Thanks for the reply, I actually already solved it just now..
I was trying many ways it turned out that this have to be 16 bit unsigned bytes...

Here are the codes for this in...

B4X:
Sub crc_calculate(Datas As String)

    Dim Buffer() As Byte
    Buffer = byte1.HexToBytes(Datas)
   
    Dim crc As Int
    Dim crcstring As String
    crc = 0x0000FFFF
    For i = 0 To Buffer.Length - 1
        crc = crc_accumulate(Buffer(i), crc)
        Log(crc)
    Next

    crcstring =  HexShift(Bit.ToHexString(crc).ToUpperCase)
    Return crcstring

End Sub

Sub crc_accumulate(Data As Byte, crc As Int) As Int

    Dim tmp, tmpdata As Int
    Dim crcaccum As Int
    crcaccum = Bit.AND(crc, 0x000000ff)
    tmpdata = Bit.AND(Data, 0x000000ff)
    tmp = Bit.Xor(tmpdata, crcaccum)
    tmp = Bit.AND(tmp, 0x000000ff)
    Dim tmp4 As Int
    tmp4 = Bit.ShiftLeft(tmp, 4)
    tmp4 = Bit.AND(tmp4, 0x000000ff)
    tmp = Bit.Xor(tmp, tmp4)
    tmp = Bit.AND(tmp, 0x000000ff)
    Dim crch As Int
    crch = Bit.ShiftRight(crc, 8)
    crch = Bit.AND(crch, 0x0000ffff)
    Dim tmp8 As Int
    tmp8 = Bit.ShiftLeft(tmp, 8)
    tmp8 = Bit.AND(tmp8, 0x0000ffff)
    Dim tmp3 As Int
    tmp3 = Bit.ShiftLeft(tmp, 3)
    tmp3 = Bit.AND(tmp3, 0x0000ffff)
    tmp4 = Bit.ShiftRight(tmp, 4)
    tmp4 = Bit.AND(tmp4, 0x0000ffff)
    Dim tmpa As Int
    tmpa = Bit.Xor(crch, tmp8)
    tmpa = Bit.AND(tmpa, 0x0000ffff)
    Dim tmpb As Int
    tmpb = Bit.Xor(tmp3, tmp4)
    tmpb = Bit.AND(tmpb, 0x0000ffff)
    crc = Bit.Xor(tmpa, tmpb)
    crc = Bit.AND(crc, 0x0000ffff)
      Return crc

End Sub

I converted it from java
Here is the source https://code.google.com/p/mavlinkjava/

Thanks anyway
Erick
 
Upvote 0

Similar Threads

Top