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?

mgordon-spiAsked:
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.

wdosanjosCommented:
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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mgordon-spiAuthor Commented:
Great Work! this is perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.