Shop OBEX P1 Docs P2 Docs Learn Events
String Question — Parallax Forums

String Question

g3cwig3cwi Posts: 262
edited 2012-04-06 11:48 in Propeller 1
Hi

I have a variable that contains a string. The string has a leading space that I want to get rid of. Assuming that the vaiable is called DIRECTION and contains " 1234", I want it to contain "1234". I thought the following code might do this but it fails.
Temp :=strsize(direction)
        
        repeat i From  0 to Temp
          y:= i+1
          direction[i] := direction[y]

Any ideas?

Regards

Richard

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-06 08:53
    Your first statement should read:
    Temp :=strsize(@direction)
    

    strsize takes an address. You were feeding it the value of the first byte. You can also use bytemove to do what you want more quickly and compactly.

    -Phil
  • g3cwig3cwi Posts: 262
    edited 2012-04-06 09:08
    Thanks Phil but it still fails.
    direction := string(" 234")
            addToPacket(direction, 4)
            
            Temp :=strsize(@direction)
            
            repeat i From  0 to Temp
              y:= i+1
              direction[i] := direction[y]
          
            addToPacket(direction, 3)
    

    Gives a response from APRS.fi of:

    " 234<0xb4><0xc4><0x04>". It correctly deals with the " 234" string but the shifted one is still wrong somehow.

    Regards

    Richard
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-06 09:19
    addToPacket also requires an address. Again, you're feeding it the first byte of the string.

    -Phil
  • AribaAriba Posts: 2,690
    edited 2012-04-06 09:52
    g3cwi wrote: »
    Thanks Phil but it still fails.
    direction := string(" 234")
            addToPacket(direction, 4)
            
            Temp :=strsize(@direction)
            
            repeat i From  0 to Temp
              y:= i+1
              direction[i] := direction[y]
          
            addToPacket(direction, 3)
    

    Gives a response from APRS.fi of:

    " 234<0xb4><0xc4><0x04>". It correctly deals with the " 234" string but the shifted one is still wrong somehow.

    Regards

    Richard

    The problem is that direction is not a string variable (this does not exist in Spin). direction is just a pointer to the string " 234" in HubMemory, and is a long. So if you do: direction := direction[y] then you move longs and not bytes and you access anyway not the string location.
    You can do it so:
    direction := string(" 234")
            addToPacket(direction, 4)
            
            bytemove(direction,direction+1,strsize(direction))
         
            addToPacket(direction, 3)
    
    but this work only one time, because you really change the string constant in memory.
    So you need a string buffer to do it correct:
    VAR
      byte buffer[16]
    
    PUB ....
    
      bytemove(@buffer,string(" 234"),5)
    
      addToPacket(@buffer, 4)
    
      bytemove(@buffer,@buffer+1,strsize(@buffer))
    
      addToPacket(@buffer, 4)
    
    I know this is a bit complicated. It will be the best to use a string library from the OBEX for string handling.

    Andy
  • g3cwig3cwi Posts: 262
    edited 2012-04-06 09:58
    Andy

    Magnificent! Worked first time. Thanks.

    @Phil - thanks for your efforts. I know far less than you assume!

    Richard
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-06 11:48
    Richard,

    My apologies for missing that direction was already an address. I was still of the mindset that it was the name of an array, even though the string keyword proved otherwise. D'oh! What was I thinking? Not enough coffee, I guess.

    Thanks, Andy, for rescuing me! :)

    -Phil
Sign In or Register to comment.