Code:
Public Function CalcChecksum(S As String) As Byte
' =====================================================================================
' === Comments by CommentMaker ===
' =====================================================================================
'
' Function : CalcChecksum
' Date/Time : wednesday 20-sep-2000 / 09:45
' By : Rob Janssen
'
' Versie : 1.0
'
' Description:
'
' Calculates a string-checksum (CRC):
'
' 1) Put 1st byte of the string in a Checksum-byte
' 2) Clear bit 7 of the resultaat
' 3) Set bit 6 of the resultaat
' 4) Add the result to the next byte (=char) of the string
' 5) Repeat steps 2-4 till the end of the string
'
'
' Parameters :
'
' S - [Required] String "To-be-checksummed"-string
'
' Return : Byte
'
' Revisions :
'
' =====================================================================================
' === Comments by CommentMaker ===
' =====================================================================================
Dim chk As Byte ' Checksum
Dim T As Long ' Char-pointer...
' Step 1
chk = Asc(Mid(S, 1, 2)) ' Get 1st Char
T = 1
While T < Len(S)
' Step 2
chk = chk And 127 ' Bitwise operation. Clear bit 7 (127 -> 01111111)
' Step 3
chk = chk Or 64 ' Bitwise operation. Set bit 6 (64 -> 01000000)
T = T + 1
' Step 4
chk = Asc(Mid(S, T, 1)) + chk
Wend
CalcChecksum = chk
End Function
usage:
Code:
Dim CRC as Byte
CRC=CalcChecksum("Hello World")