PDA

View Full Version : Type Casting/Conversion



Dizzy
04-24-2008, 11:16 PM
Hello again,

I'm modifying some code where I need to store the value from a byte into a long. I suppose I could do this with shifts and the like but is there a simpler way? I've tried for instance: LONG[v126] := BYTE[packet1]

I can't tell if that works or not.. according to my test program it doesn't haha. Is there an easier cast or a simple trick that anyone knows who has run into this?

Thanks,
D.

Mike Green
04-24-2008, 11:37 PM
If "v126" is a LONG and "packet1" is a BYTE, you just need to write "v126 := packet1". When the Spin interpreter fetches a value, it's converted to a 32-bit value (to store on the stack). What you wrote, "LONG[v126] := BYTE[packet1]", means take the byte value at the address contained in "packet1", extend it to 32 bits and store it in the long variable at the address contained in "v126".

Paul Baker
04-25-2008, 03:50 AM
If you are using signed numbers (-128 to 127) then be sure to sign extend by using ~BYTE[packet1].

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)