# Android Code SnippetCRC 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``````

#### Erel

##### B4X founder
Staff member
Longtime User
Moved to the code snippets forum.

Android Question checksum ccitt
Replies
2
Views
399
Other CRC CCITT
Replies
1
Views
651
Android Question CRC-CCITT (XModem)
Replies
4
Views
1K
Android Question String CRC32
Replies
3
Views
3K
Android Question Modbus CRC16 calculation
Replies
13
Views
5K