Hi, i have this code example in VB that calculate a checksum for serial communication with a balance.
Public Function CalCksum(StrDat As String) As String
Dim i,sum,app As Integer
sum = 0
For i = 1 To Len(StrDat)
sum = sum + Asc(Mid(StrDat, i, 1))
If (sum > 256) Then sum = sum - 256
Next i
app = ((sum And &HF) + (sum And &HF0) / 16) And &HF
app = app Or &H30
If (app > &H39) Then app = app + 7
CalCksum = Chr(app)
End Function
i’ve tried to translate in XOJO with this code:
Dim i,sum,app As Integer
sum = 0
For i = 1 To Len(TextField1.Text)
sum = sum + Asc(Mid(TextField1.Text, i, 1))
If (sum > 256) Then sum = sum - 256
Next i
app = ((sum And Integer.FromHex("hF") ) + (sum And Integer.FromHex("hF0")) / 16) And &HF
app = app Or &H30
If (app > &H39) Then app = app + 7
Label1.Text = str(app)
but i’ve got this compiling error:
Undefined operator. Type Double does not define “And” with type UInt32
and “assuming” you are NOT going to worry about multi-byte UTF-8 characters, then replace ASC() with ASCB()
but if you text DOES contain Unicode then ASC may return a value > 255 to start with
I think you should replace “/ 16” with “\ 16”, this way there isn’t created a double by dividing an integer. I can not test it right now because i am not at my dev-pc, but it’s worth a try.
Public Function CalCksum(StrDat As String) as String
Dim i,sum,app As Integer
sum = 0
For i = 1 To LenB(StrDat)
sum = sum + AscB(MidB(StrDat, i, 1))
If (sum > 256) Then sum = sum - 256
Next i
app = ((sum And &HF) + (sum And &HF0) \\ 16) And &HF
app = app Or &H30
If (app > &H39) Then app = app + 7
return ChrB(app)
End Function