-
I need help in creating a 'checksum' from a string using the following conventions:
1) Add the 1st byte of the string to a checksum byte
2) Clear Bit 7 of the above result
3) Set Bit 6 of the result
4) Add this result to the next byte of the string
5) Repeat steps 2-4 until last byte of string is read.
Please Help! I don't know how to get started?!
-
Try this one...
Code:
Option Explicit
Private Sub Form_Load()
Debug.Print CalcChecksum("Hello World!")
End Sub
Public Function CalcChecksum(S As String) As Byte
Dim chk As Byte ' Final Checksum
Dim T As Long ' Char-pointer...
' Step 1
chk = Asc(Mid(S, 1, 2)) ' Get first Char value
' Initialize counter
T = 1
While T < Len(S)
' Step 2
chk = chk And 127 ' Bitwise operation. Clear bit 7 (127 -> 0111 1111)
' Step 3
chk = chk Or 64 ' Bitwise operation. Set bit 6 (64 -> 01000000)
' Increase counter...
T = T + 1
' Step 4
chk = Asc(Mid(S, T, 1)) + chk
Wend
CalcChecksum = chk
End Function
Hope it works for you...