Calculating CheckSum from String with VB6

IP 41 Reputation points
2022-05-26T14:43:30.663+00:00

Hi there,

I need help please calculating the checksum of a string on Visual Basic 6!

String = "50 44 57 31 32" the checksum should be 14E but I cannot figure out a way of writing the VB6 formula.

In my code, I manipulate the string message, convert to HEX and then should calculate the CheckSum + add (i.e. not mathematical addition) to "newString"

My VB6 code:

Public Sub sendPDW()

Dim strTemp As String
Dim strTempPDWsData As String
Dim strTempPDWsData2 As String
Dim strReturn As String
Dim I As Long
Dim SendPDW As String
Dim strToHex As String
Dim hexTemp2 As String
'Declare CheckSum Variables... as Long?
...

...

PDWsData = " 12"

'STRING MANIPULATION BY REMOVING THE SPACE
strTempPDWsData = Mid$(PDWsData, 2) 'PDWsData = "12"

'COMBINE WITH COMMAND
strTempPDWsData2 = "PDW" & strTempPDWsData 'strTempPDWsData2 = "PDW12"

'STRING DATA CONVERSION TO HEX
For I = 1 To Len(strTempPDWsData2)
strTemp = Hex$(Asc(Mid$(strTempPDWsData2, I, 1)))
If Len(strTemp) = 1 Then strTemp = "0" & strTemp
strToHex = strToHex & Space$(1) & strTemp
Next I

'strToHex = " 50 44 57 31 32" & "CS"

'STRING MANIPULATION BY REMOVING THE SPACE
hexTemp2 = Mid$(strToHex, 2) 'hexTemp2 = "50 44 57 31 32 CS"

...
'CheckSum Calculation of hexTemp2 in VB6 + Replace CS with CheckSum value
'Define code here

'CheckSum value = ????
'hexTemp2 + CheckSum value = newString
'Expected newString = "50 44 57 31 32 14E"
...

SendPDW = newString

If Winsock1.State = 7 Then
Winsock1.SendData (SendPDW)
End If

End Sub

Thank you!!!

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-05-31T05:55:06.3+00:00

    Hi

    Since this is a "VB dot NET" forum, here is code that may help (it's up to you to amend for VB6)

     Dim input As String = "50 44 57 31 32"
    
     Dim hx() As String = Split(input, " "c)
    
     Dim tot As Integer = 0
     For i As Integer = 0 To hx.Count - 1
     tot += Convert.ToInt32(hx(i), 16)
     Next
    
     Dim res As String = Hex(tot)
     ' tot =  334  (Integer) = 14E  (Hex)
     ' res = "14E" (string)
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.