Shop OBEX P1 Docs P2 Docs Learn Events
Accessing string elements, casting? — Parallax Forums

Accessing string elements, casting?

GrendelTGrendelT Posts: 23
edited 2012-08-06 22:33 in Propeller 1
In the following code, I've created a string using a long the string( ) call, and I've created a byte array to form the string "spin".
CON _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
OBJ pst : "Parallax Serial Terminal"
VAR byte foo[10]
    long bar[10]
PUB main
    pst.Start(115200)

    foo[0] := "s"
    foo[1] := "p"
    foo[2] := "i"
    foo[3] := "n"
    foo[4] := 0

    bar := string("fun")
    
    pst.str(foo[1])
    pst.newline
    pst.str(bar)

Why on the pst.str(foo[1]) does it not print p? If I include the @ operator to make it @foo[1], I get pin, not just the p. How would I pull only the p from foo?

Do I need to do some casting trickery on it? Is that what the byte[ ] syntax I've seen used on others' code? Casting? If not, please explain what that means, I'm having a hard time finding it. (or at least tell me the name of it so I can look it up.)

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-08-06 21:46
    Printing a single character requires a different method (after all you're not printing a string anymore). Use pst.char(foo[1]) (other objects use tx, out or putc). Also, you only need a single long to store a string address.

    If you have a closer look at the used objects you'll find that the str method is usually based on the single character output method, e.g.
    PUB [COLOR="blue"]str[/COLOR](addr)
    
      repeat strsize(addr)
        [COLOR="orange"]out[/COLOR](byte[addr++])
    
  • GrendelTGrendelT Posts: 23
    edited 2012-08-06 22:12
    Awesome! Thanks.

    How about the byte[ ] or word[ ] syntax I see used from time to time? What's that called/doing?
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-06 22:18
    GrendelT wrote: »
    How about the byte[ ] or word[ ] syntax I see used from time to time? What's that called/doing?
    If you take the method above as an example, all it knows about is an address (of a string). IOW it doesn't have a name of a global/local variable to refer to (so @foo wouldn't work). Which is where byte[] & Co come in. They take an address (which has been communicated to this method) and return the relevant byte/word/long from hub memory (within alignment rules, e.g. a long is always loaded from 4n).
    VAR long example, addr
    
      example := 42
      long[@example] := 42
    
      addr := @example
      long[addr] := 42
    
    All three assignments do the same thing (loading VAR example with 42).
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-06 22:33
    Note, there is another form which is used to access e.g. bytes within a long like
    VAR long example, n
    
      example := $12345678
      n := extract(example, 3)
    
    PUB extract(value, byte_index)
    
      return value.byte[byte_index]
    
    While wrapping this into a method is clearly overkill, it should demonstrate the principle. The above is equivalent to byte[@value][byte_index].
Sign In or Register to comment.