Shop OBEX P1 Docs P2 Docs Learn Events
Data in byte-sized vs. long-sized variables — Parallax Forums

Data in byte-sized vs. long-sized variables

coryco2coryco2 Posts: 107
edited 2013-07-21 06:01 in Propeller 1
Is it not possible to assign a value stored in a long-sized variable to a byte-sized variable, providing the value is no more than 255?

byteSizedVariable:= longSizedVariable

For some reason it doesn't seem to work that way. I then tried

byteSizedVariable:= longSizedVariable.byte[0]

to assign the least significant byte of the long value, but no go.

What am I missing here?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-20 07:53
    byteSizedVariable := longSizedVariable

    This does work as does

    byteSizedVariable := longSizedVariable.byte[ 0 ]

    The "no go" must be due to something else that we can't tell with the amount of information given. Maybe you misspelled the variable name?
  • coryco2coryco2 Posts: 107
    edited 2013-07-20 08:29
    Hmm. Okay, thanks.

    It appears that I am running into my trouble writing bytes to a DAT data table. Is there a difference between

    byte[@dataTable1][0] := "0"

    and

    byte[@dataTable1][0] := 0

    as far as the value that is written to byte in dataTable?
  • SRLMSRLM Posts: 5,045
    edited 2013-07-20 08:35
    "0" writes the ASCII value for the character 0 (zero), while 0 writes the value zero (ie, all bits cleared).
  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-20 08:38
    Sure, they're different values. 0 is the value zero. "0" is the character zero which is $30 or 48 in ASCII (the current standard for single byte characters).
  • coryco2coryco2 Posts: 107
    edited 2013-07-20 08:49
    Ok. So something like this:

    byte[@dataTable1][0] := "0"+ Variable1

    will just add the ASCII value of character 0 to the numeric value of Variable1? And what will the result be in the table? A number or a character string?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-20 09:08
    The object "Numbers" in the Prop Tool library has methods for converting numbers to strings and back.

    LocalRoger just posted an object which also does these things.

    Make sure you get what's going on straight in your head. It took me a while to make sense of ASCII characters verses the values used by the Prop internally. I see a lot of forum members have trouble with this and there has been a lot of discussion on this issue. I'll try to find some links to past threads and post them here.
  • coryco2coryco2 Posts: 107
    edited 2013-07-20 09:12
    Ok, thanks.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-20 18:14
    LocalRoger's thread is here. His object includes methods to load variable values into a buffer.
  • localrogerlocalroger Posts: 3,451
    edited 2013-07-21 06:01
    I have a more recent thread which demonstrates some more convenient and powerful usages, and also adds a val() method to StrFmt for converting ASCII strings back to numbers:

    http://forums.parallax.com/showthread.php/149189-UPDATED-with-example-Modular-Programming-in-Spin
Sign In or Register to comment.