Question Need to build CRC-16 CCITT calculator

justinshn

New member
Joined
Oct 22, 2010
Messages
1
Programming Experience
Beginner
Hello,

I'm trying to figure out how to modify some code to create an Excel calculator that has been set up for CRC XMODEM, to calculate for the CCITT polynomial x16 + x12 + x5 + 1

I changed the H8408 to H1021 but I'm getting the wrong calculation.

To test the code I enter the string 83FED3407A93 and I know the CRC is supposed to be, 0x702B which I then need to reverse the order to read as 0x2B70.

This is the code...

VB.NET:
Private Function CRC16(ByVal crc As Integer, d As Byte) As Integer
  Dim carry As Integer, i As Byte
  For i = 0 To 7
    carry = (crc And 1) Xor IIf(d And (2 ^ i), 1, 0)
    crc = (crc And &HFFFF&) \ 2
    If carry <> 0 Then crc = crc Xor &H8408
  Next i
  CRC16 = crc
End Function

Public Function CalcCrc16(ByVal buf As String) As String
  Dim crc As Integer, t As Integer
  crc = &H8408
  For t = 1 To Len(buf)
    crc = CRC16(crc, Asc(Mid$(buf, t, 1)))
  Next t
  CalcCrc16 = Hex$(crc)
End Function

Public Function CalcCrc16R(ByVal buf As String) As String
  Dim crc As Integer, t As Integer
  crc = &H8408
  For t = 1 To Len(buf)
    crc = CRC16(crc, Asc(Mid$(buf, t, 1)))
  Next t
  CalcCrc16R = Right(Right("0000" & Hex$(crc), 4), 2) & Left(Right("0000" & Hex$(crc), 4), 2)
End Function

If it helps figure out how to get the value I’m getting, when using this calculator - CRC calculation - with the following parameters

1) Click the CCITT button
2) Enter FFFF for the “Final XOR value”
3) select the "reverse data bytes" box and the "reverse CRC results before final XOR" box
4) Enter the string, separated with “%” signs - %83%FE%D3%40%7a%93 for a string of 83FED3407A93...the result produced is 702B, which I then need to reverse to read 2B70.
 
Back
Top