Home AMX User Forum NetLinx Studio

Checksum Question - Part 2

Working with this:

define_function integer getChecksum(char cmd[])
{
stack_var integer bTotal
stack_var integer nLength
stack_var integer nCT

nLength = LENGTH_STRING(cmd - 1)
nCT = 1

for(nCT=1;nCT<=8;nCT++)
{
bTotal = bTotal + cmd[nCT]
}
bTotal = bTotal & $FF

return bTotal
}

Getting this:

'String used as a CHAR value in a math operation' in relation to the line that says 'nLength = LENGTH_STRING(cmd - 1)'

I'm stumped. I'm trying to get rid of these warnings so I can learn more about this part of the programming, but I'm at a loss. I just fixed a handful of similar problems and learned a lot in the process, but this one isn't coming to me.

Comments

  • ericmedleyericmedley Posts: 4,177
    nLength = LENGTH_STRING(cmd - 1)

    Should be
    nLength = LENGTH_STRING(cmd)-1
  • That did it! It seems obvious, now, but I just wasn't thinking about it properly. Thanks!
  • ericmedleyericmedley Posts: 4,177
    your welcome.
  • svTechsvTech Posts: 44
    I would strongly suggest that you initialize bTotal before your FOR loop. If your cmd[] comes in with 0 length, you won't necessarily know what gets returned, unless a STACK_VAR is always initialized to "0". It's just good practice to initialize the return variable before you make any decisions on it or calculate a new value, in my opinion.
Sign In or Register to comment.