Shop OBEX P1 Docs P2 Docs Learn Events
Type Casting/Conversion — Parallax Forums

Type Casting/Conversion

DizzyDizzy Posts: 9
edited 2008-04-24 19:50 in Propeller 1
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[noparse][[/noparse]v126] := BYTE[noparse][[/noparse]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.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-24 15:37
    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[noparse][[/noparse]v126] := BYTE[noparse][[/noparse]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 BakerPaul Baker Posts: 6,351
    edited 2008-04-24 19:50
    If you are using signed numbers (-128 to 127) then be sure to sign extend by using ~BYTE[noparse][[/noparse]packet1].

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.