PDA

View Full Version : String Question



g3cwi
04-06-2012, 04:47 PM
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

Phil Pilgrim (PhiPi)
04-06-2012, 04:53 PM
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

g3cwi
04-06-2012, 05:08 PM
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)
04-06-2012, 05:19 PM
addToPacket also requires an address. Again, you're feeding it the first byte of the string.

-Phil

Ariba
04-06-2012, 05:52 PM
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[i] := 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

g3cwi
04-06-2012, 05:58 PM
Andy

Magnificent! Worked first time. Thanks.

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

Richard

Phil Pilgrim (PhiPi)
04-06-2012, 07:48 PM
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