Shop OBEX P1 Docs P2 Docs Learn Events
Checksum help — Parallax Forums

Checksum help

Don MDon M Posts: 1,652
edited 2012-08-19 17:42 in Propeller 1
I am trying to calculate a checksum from a series of bytes. The check byte is limited to 8 bits so the carry bit is ignored.

This code works:
chk & s are longs
repeat pos from 0 to 22
  s := byte[@array1][pos]
  pst.tx(s)
  chk += s
pst.tx(chk)

dat

array1  byte  $03, $00, $01, $05, $02, $00, $0F, $01, $02, $05, $14, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00

chk = 36 which is correct.

However this one does not work:
chk & s are longs
repeat pos from 0 to 32
  s := byte[@array1][pos]
  pst.tx(s)
  chk += s
pst.tx(chk)

dat 

array1  byte  $4D, $45, $49, $30, $31, $38, $38, $59, $53, $30, $31, $30, $37, $30, $20, $43, $46, $37, $35, $30, $30, $4D, $44, $42, $20, $20, $20, $01, $18, $00, $00, $00, $07

chk should be 18 but it shows 4E and then next time 66

What am I doing wrong?

Thanks.
Don

Comments

  • RaymanRayman Posts: 14,671
    edited 2012-08-19 17:09
    The fact that it changes is maybe a clue?... is chk set to zero before the start?
  • Don MDon M Posts: 1,652
    edited 2012-08-19 17:15
    @Ray-

    No it wasn't. It is now and it works. Thanks. You're a genius. I feel rather silly about this.

    Thanks again.
    Don
  • StephenMooreStephenMoore Posts: 188
    edited 2012-08-19 17:33
    glad its solved
  • RaymanRayman Posts: 14,671
    edited 2012-08-19 17:42
    Glad I could help. I think Spin usually initialized things to zero automatically, so sometimes it's hard to tell when you need to do it yourself...
Sign In or Register to comment.