conversion from VB

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

on line 7 of my code. Someone can help me?

thanks

Line #7 - use exactly the same syntax as VB did

no need for

Integer.FromHex("hF")

when &HF works just as well

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

Thanks Dave,

if i take line #7 as is:

app = ((sum And &hF ) + (sum And  &hF0) / 16) And &HF

i’ve got Type Double does not define “And” with type Uint32

The problem is the last part of line #7, And &HF

without this no error, obviously the formula doesn’t work

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.

Yes, what Andre says will fix it. Combine that with what Dave suggests and your line 7 looks like this:

app = ((sum And &hF) + (sum And &hF0) \\ 16) And &hF

yeah, so obvious… : ))

thanks Dave,Paul & Andre

The VB code almost runs exactly as is

  • changed to use lenb, ascb, midb etc
  • changed final line to use “return”
  • changed / 16 to \ 16
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