# Calculate CRC-CITT from Byte Array

Hi All,

I need to communicate with a small IP device accross a WAN link and its requirement is to send a CRC along with the command to process. I've been supplied with the folowing formula

0x05d8 = CRC-CCITT (24 24 00 11 13 61 23 45 67 8f ff 50 00)

I've tried what I know to convert and work with that formula, but I cannot seem to calculate the CRC corrently, the nearest I can get is 3232333136?

Is anyone able to help me with a function that will return a byte array that has the correct CRC calculation for a given byte array?

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Please try the following code.  It returns 0x05D8 for you sample byte array.

``````Sub Main
Dim bytes As Byte() = New Byte() {&H24, &H24, &H00, &H11, &H13, &H61, &H23, &H45, &H67, &H8f, &Hff, &H50, &H00}
Dim crc As Byte()

crc = CRCCCITT ( bytes )

Console.WriteLine("0x{0:X2}{1:X2}", crc(0), crc(1))
End Sub

Function CRCCCITT(bytes As Byte()) As Byte()
Dim crc As Integer = &HFFFF		   ' initial value
Dim polynomial As Integer = &H1021 ' 0001 0000 0010 0001  (0, 5, 12)
Dim b As Byte
Dim i As Integer

For Each b in bytes
For i = 0 To 7
Dim bit As Boolean = (((b >> (7 - i)) And 1) = 1)
Dim c15 As Boolean = (((crc >> 15) And 1) = 1)

crc = crc << 1

If (c15 XOr bit) Then
crc = crc XOr polynomial
End If
Next
Next

crc = crc And &HFFFF

Return New Byte() { (crc >> 8), (crc And &HFF) }
End Function
``````

Experts Exchange Solution brought to you by