Android Code Snippet CRC CCITT (xFFFF) Calculation

CRC CCITT (0xFFFF) calculation on https://www.lammertbies.nl/comm/info/crc-calculation.html

Here is the code:
B4X:
Private Sub CalculateCRC(Data() As Byte) As Int
    Dim i, j As Int
    Dim lngCheck As Long
    Dim Poly, CRC As Long
    Dim TestBit, TestBit1, TestBit2 As Boolean

    Poly = 0x1021
    CRC = 0xFFFF
    
    For i = 0 To Data.Length - 1
        lngCheck = Data(i)
        
        For j = 7 To 0 Step -1
            If Bit.And(CRC, 32768) = 32768 Then TestBit1 = True Else TestBit1 = False
            If Bit.And(lngCheck, Power(2, j)) = Power(2, j) Then TestBit2 = True Else TestBit2 = False

            TestBit = TestBit1 <> TestBit2
            CRC = Bit.And(CRC, 32767) * 2
            
            If TestBit Then CRC = Bit.xor(CRC, Poly)
        Next
    Next

    Return CRC
    
End Sub
 

Similar Threads

Replies
34
Views
4K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
4
Views
1K
Top