What's up with this?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

What's up with this?

Post by Joakim Christiansen »

Code: Select all

EnableExplicit

Define string$, i, checksum.a, calculatedChecksum.a

string$="10003000FCCF3091C600089528E02EBF2FEF2DBFD2"
checksum = Val("$"+Right(string$,2))
For i=0 To 19
  calculatedChecksum + Val("$"+Mid(string$,i*2+1,2))
Next
If ~calculatedChecksum+1 = checksum
  Debug "match"
Else
  Debug "no match"
EndIf
Debug checksum
Debug ~calculatedChecksum+1
Change .a to .b and it works, can someone explain why it wont work if using unsigned bytes? :?
I like logic, hence I dislike humans but love computers.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: What's up with this?

Post by Trond »

Calculations are always done signed in PB, you have to store the value in an unsigned value to treat it as unsigned.

Code: Select all

EnableExplicit

Define string$, i, checksum.a, calculatedChecksum.a

string$="10003000FCCF3091C600089528E02EBF2FEF2DBFD2"
checksum = Val("$"+Right(string$,2))
For i=0 To 19
  calculatedChecksum + Val("$"+Mid(string$,i*2+1,2))
Next
Define checksum2.a = ~calculatedChecksum+1
If checksum2 = checksum
  Debug "match"
Else
  Debug "no match"
EndIf
Debug checksum
Debug checksum2
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: What's up with this?

Post by Joakim Christiansen »

Aha, so the If when comparing temporary stores the value ~calculatedChecksum+1 as a signed value when it does the comparison (if I got that right).

This also fixes it:

Code: Select all

calculatedChecksum = ~calculatedChecksum+1
If calculatedChecksum = checksum
Thanks! :)
Now...to remember this everywhere in my code :shock:
I like logic, hence I dislike humans but love computers.
Zach
Addict
Addict
Posts: 1678
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: What's up with this?

Post by Zach »

Find / Replace :mrgreen:
Post Reply