Android Question CRC-CCITT (XModem)

sales

Member
Licensed User
Longtime User
=====

Sub ComputeCRC (val() As Byte) As Byte()
Dim crc As Long
Dim q As Long
Dim c As Byte
For i = 0 To val.Length - 1
c = val(i)
q = Bit.And(Bit.Xor(crc, c), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
Next
Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub

=====

that code will generate CRC-CCITT (Kermit) from: FC0511 CRC: 2756

then i need to get CRC-CCITT (XModem) from: FC0511 expected CRC: 6BD6

https://www.lammertbies.nl/comm/info/crc-calculation.html


which part of the code should be modified?

many thanks
 

JordiCP

Expert
Licensed User
Longtime User
then i need to get CRC-CCITT (XModem) from: FC0511 expected CRC: 6BD6
I tested it and works
B4X:
Sub calcCRC( data() As Byte, num As Int) As Int
 
   Dim crc As Int=0
   For k=0 To num-1
     crc = Bit.Xor( crc, Bit.ShiftLeft( data(k),8) )
     For i=0 To 7
       crc = Bit.ShiftLeft(crc,1)
       If Bit.And(crc,0x10000)<>0 Then
         crc = Bit.Xor(crc,0x1021)
       End If
     Next   
   Next
   crc = Bit.And(crc,0xFFFF)   'need to clean since the container was bigger
   Return crc
End Sub
 
Upvote 0

sales

Member
Licensed User
Longtime User
I tested it and works
B4X:
Sub calcCRC( data() As Byte, num As Int) As Int

   Dim crc As Int=0
   For k=0 To num-1
     crc = Bit.Xor( crc, Bit.ShiftLeft( data(k),8) )
     For i=0 To 7
       crc = Bit.ShiftLeft(crc,1)
       If Bit.And(crc,0x10000)<>0 Then
         crc = Bit.Xor(crc,0x1021)
       End If
     Next  
   Next
   crc = Bit.And(crc,0xFFFF)   'need to clean since the container was bigger
   Return crc
End Sub


hi JordiCP,

i was try with this code: but error

Dim FstLine As String="FC0511"
Dim Secline As String= bc.HexFromBytes(calcCRC(bc.HexToBytes(FstLine)))
Log(Secline)


error:

Error compiling program.
Error description: ',' expected.
Error occurred on line: 69
Dim Secline As String= bc.HexFromBytes(calcCRC(bc.HexToBytes(FstLine)))
Word: )



sorry i'm still beginner


thanks
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
The code returns an integer instead of an array of bytes, and the function expects the length (in this case, 3) as a parameter.
If you want to get its hex representation, you can do

B4X:
Dim FstLine As String="FC0511"
Dim Secline As String= Bit.ToHexString(calcCRC(bc.HexToBytes(FstLine),3))
Log(Secline)
 
Upvote 0

sales

Member
Licensed User
Longtime User
The code returns an integer instead of an array of bytes, and the function expects the length (in this case, 3) as a parameter.
If you want to get its hex representation, you can do

B4X:
Dim FstLine As String="FC0511"
Dim Secline As String= Bit.ToHexString(calcCRC(bc.HexToBytes(FstLine),3))
Log(Secline)

ok it work ok,


many thanks JordiCP
 
Upvote 0

Similar Threads

Replies
2
Views
1K
  • Question
Android Question xmodem-crc
Replies
12
Views
2K
  • Question
Android Question String CRC32
Replies
3
Views
4K
Top