Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

crc16 ccitt calculation

Status
Not open for further replies.

hottoh

Programmer
Joined
Aug 3, 2003
Messages
1
Location
BH
ccitt crc16 algoritham

the polynomial for this algorithm is X**16+X**12+X**5+1


D = current data byte that is input to the algorithm

CRCLSB,CRCMSB=data byte.least significant and most significant CRC accumulator
bytes

x>>n means x is shifted n bits to right or is the same as x divided by 2 to the power n

x<<n means x is shifted n bits to left or is the same as x multiplied by 2 to the power n



======================algoritham=======================================

at beginning,
CRCLSB = 0FFH(octal 377)(decimal 255)
CRCMSB = 0FFH(octal 377)(decimal 255)
then for each data byte in a block,
X=D XOR CRCMSB
x=x xor (X>>4)
CRCMSB=CRCLSB XOR(X>>3)XOR(X<<4)
CRCLSB=X XOR(X<<5)
and at end,
CRCLSB=CRCLSB XOR 0FFH
CRCMSB=CRCMSB XOR 0FFH

======================this is vb6 program for above algoritham=======================================



Public Function CRC16A(Buffer() As Byte) As Long

Dim i As Long
Dim Temp As Long
Dim crc As Long
Dim j As Integer
crc = &HFFFF&
For i = LBound(Buffer) To UBound(Buffer)
Temp = Buffer(i) * &H100&
crc = crc Xor Temp
For j = 0 To 255
If (crc And &H8000&) Then
crc = ((crc * 2) Xor &H1021&) And &HFFFF&
Else
crc = (crc * 2) And &HFFFF&
End If
Next j
Next i
CRC16A = crc And &HFFFF

End Function


Private Sub Command1_Click()

Dim aBuf() As Byte

Dim crc As Long
Dim p As String

p = Text1.Text
aBuf = StrConv(p, vbFromUnicode)
crc = CRC16A(aBuf)
Text2.Text = Hex(crc)

End Sub


===============could you help to get correct answer=====
 
Hi,

I'm searching for a normal CRC16 code does somebody have a working version?
 
I have found this code but I cann't get it work in VB6
I hope somebody could help me out :)

'This Function is used to format output from hex calculation
'Into a format of iwidth string. This is sometimes neccesary to return also
the
'Hight order byte zero.
Public Function FmtHex(ByVal i As Long, Optional iwidth As Long = 0) As
String
If iwidth &lt;= 0 Then
FmtHex = Hex(i)
Else
FmtHex = Right(String(iwidth, &quot;0&quot;) &amp; Hex(i), iwidth)
End If
End Function



Option Explicit
Global gl_crc16 As Long


Public Function DoCrc16(ByVal dat As Long) As Long
'This function calculates the crc16 of a long byte
'make sure that the global value gl_crc16 is 0 before starting
dat = (dat Xor (gl_crc16 And 255)) And 255
gl_crc16 = (gl_crc16 And 65280) / 256
If (odd_parity(dat) = 1) Then gl_crc16 = gl_crc16 Xor 49153
dat = (dat * 64) And 65535
gl_crc16 = gl_crc16 Xor dat
dat = (dat * 2) And 65535
gl_crc16 = gl_crc16 Xor dat
gl_crc16 = gl_crc16
DoCrc16 = gl_crc16
End Function
Private Function odd_parity(ByVal dat As Long) As Long
Dim n As Long
Dim i As Long

n = 0
For i = 0 To 7
n = n + dat And 1
dat = (dat And 65534) / 2
Next i
odd_parity = n And 1
End Function

 
What line is the compiler complaining about?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Just convert the HTML bits back to standard:
For instance replace:
If iwidth &lt;= 0 Then
with
If iwidth <= 0 Then

and replace:
FmtHex = Right(String(iwidth, &quot;0&quot;) &amp; Hex(i), iwidth)
with
FmtHex = Right(String(iwidth, "0") & Hex(i), iwidth)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top