You might want to look at the STRSIZE and BYTEMOVE inbuilt functions in the Propeller manual.
You'll need to be sure the TxString array is big enough to hold the incoming strings.
By the way. If you ever want to run a repeat loop over a string for whatever reason it's better to get the length into a variable, say "slen" and then do a repeat loop that many times. Because as it stands you are calling STRSIZE every time around the repeat and STRSIZE will loop around the string every time counting the bytes.
Edit: ERROR. The above is wrong. The repeat expression is only evaluated once. Sorry. See later post.
You'll need to BYTEMOVE STRSIZE+1 bytes to include the zero terminator, in case the receiving string doesn't have one, or if it's located somewhere else.
By the way. If you ever want to run a repeat loop over a string for whatever reason it's better to get the length into a variable, say "slen" and then do a repeat loop that many times. Because as it stands you are calling STRSIZE every time around the repeat and STRSIZE will loop around the string every time counting the bytes.
This is not true. If you use the REPEAT <Count> method, it only evaluates <Count> once (See Page 190 of Manual v1.1). So storing the string length in a variable will actually slow down the overall process (by about 360 cycles).
This is not true. If you use the REPEAT <Count> method, it only evaluates <Count> once (See Page 190 of Manual v1.1). So storing the string length in a variable will actually slow down the overall process (by about 360 cycles).
Yes, "repeat strsize(str)" will initialize the loop count at the beginning. If strsize is zero it will not execute the loop.
Now if you did "repeat i from 1 to strsize(str)" it would call strsize at the end of each loop. And if the value of strsize was zero it would actually execute the loop twice, with values of 1 and 0 for i.
To make up for it, notice that if you do actually want the expression in the repeat statement to be re-evaluated each time around the loop then you can use "repeat while"
The first repeat goes around 3 times as the repeat expression is evaluated only once and is 3 after that the 3 is decremented each loop.
The second repeat goes around forever as the expression is evaluated every time and the while condition keeps getting reset to 3. As it were.
Yes, and that was heater first suggestion without the + 1, and then Phil came in and added a comment about the +1. That is a lot neater than my first way of doing it.
is there a better way of updating a string than this?
PUB UpdateTxString(NewTxStringPointer) | Char, Index
Index := 0
REPEAT STRSIZE(NewTxStringPointer)
Char := NewTxStringPointer++
TxString[Index] := Char
Index++
e
Bruce,
The original code you posted wouldn't work the way you want it to. You need to use a byte[] intrinsic to access the data in NewTxStringPointer. So the code should look like this:
PUB UpdateTxString(NewTxStringPointer) | Index
Index := 0
REPEAT STRSIZE(NewTxStringPointer)
TxString[Index++] := byte[NewTxStringPointer++]
I'm assuming TxString was defined as a byte array in a DAT or VAR section.
Comments
You'll need to be sure the TxString array is big enough to hold the incoming strings.
Application specific.... The string can be no longer than 21 characters.
Bruce
Edit: ERROR. The above is wrong. The repeat expression is only evaluated once. Sorry. See later post.
That makes perfect sense to me. Thanks for the tip.
Bruce
-Phil
Yea, that works perfectly and looks much better.
Bruce
Thanks for that tip, I am sure it will come in handy in the future.
Bruce
Got it thanks
Bruce
Now if you did "repeat i from 1 to strsize(str)" it would call strsize at the end of each loop. And if the value of strsize was zero it would actually execute the loop twice, with values of 1 and 0 for i.
Shame on you for giving the newbie bad advice
I forgive you though
Bruce
Sorry, yes, cardinal sin.
To make up for it, notice that if you do actually want the expression in the repeat statement to be re-evaluated each time around the loop then you can use "repeat while"
For example in the following:
The first repeat goes around 3 times as the repeat expression is evaluated only once and is 3 after that the 3 is decremented each loop.
The second repeat goes around forever as the expression is evaluated every time and the while condition keeps getting reset to 3. As it were.
This is TRUE. I tested it:)
Yes, and that was heater first suggestion without the + 1, and then Phil came in and added a comment about the +1. That is a lot neater than my first way of doing it.
Bruce
I am glad I asked this question, it was nice, just to find a better way, but finding out more with your test loop was also cool.
Bruce
The original code you posted wouldn't work the way you want it to. You need to use a byte[] intrinsic to access the data in NewTxStringPointer. So the code should look like this:
I'm assuming TxString was defined as a byte array in a DAT or VAR section.
Dave