PDA

View Full Version : I need to learn how to make a string



Brian Carpenter
03-30-2009, 12:02 PM
i have several variables that i need to make into a string with commas separating the values. eed to then count the length of the string and send the sting out serially. I can send the data but i am not sure how to create the string. See below



Digi.str(string("$THERM,"))
Digi.hex(mach_ID,4)
Digi.str(string(","))
Digi.dec(Therm1H)
Digi.str(string(","))
Digi.dec(Therm2H)
Digi.str(string(","))
Digi.dec(Therm3H)
Digi.str(string(","))
Digi.dec(Therm4H)
Digi.str(string(","))
Digi.dec(CSP1)
Digi.str(string(","))
Digi.dec(CSP2)
Digi.str(string(","))
Digi.dec( Amps1)
Digi.str(string(","))
Digi.dec(Amps2)
Digi.str(string(","))
Digi.dec(contact1)
Digi.str(string(","))
Digi.dec(contact2)
Digi.str(string(","))
Digi.dec(contact3)





Any ideas or threads to read would be great.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Mike Green
03-30-2009, 12:33 PM
Why do you need to create the string? Is it only because you don't know the length of the string? That's pretty easy. You know the lengths of all the character and string segments and the hexadecimal segment. You just need the length of the decimal numbers. How about:


PUB decLength(v)
if v < 0 ' If negative, account for sign
result++
||v
repeat while v => 10 ' Reduce to < 10
result++ ' How many digits needed?
v /= 10
result++ ' Last digit

Brian Carpenter
03-30-2009, 01:07 PM
Mike
So i would run this routine for each dec value and then add it all together with the known number of other characters?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

MagIO2
03-30-2009, 02:07 PM
Why do you need the length at all?

You could simply define one "LineEnd" character. For direct terminal-output this could be the 13 (Carriage Return).
The receiver has a buffer which is long enough to carry the max. number of characters you expect and receives until the buffer-length has been reached or it receives the line-end character. If the receiver reached the buffer-end it continues with reading until line-end, but does not store the characters anywhere. This should never happen in normal operation, but this way your receiver is failsafe and should not have buffer overflow bugs.

If the string is not for direct output, you could transmit binary values instead. Then you have a fixed size for each transmission. Say your values have word size, then you have to transmit 12*2 bytes.

Brian Carpenter
03-31-2009, 01:21 PM
i am using it in an HTTP POST application and must know the length

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Mike Green
03-31-2009, 01:43 PM
There are all sorts of ways to do this. What I suggested is, in some ways, the simplest. Another thing to do would be to take the .str / .hex / .dec / .bin code from one of the display objects and modify it to put the characters in a buffer using a zero terminator. Each routine would add to the end of the string and you'd have a method to return the address of the buffer and another to clear it (by storing a zero byte at the beginning. This would let you edit the text line the way you're used to and accumulate the characters to use in one single output string and you could precompute the length as well.

jazzed
03-31-2009, 02:26 PM
Brian, Have a look at the Format object http://obex.parallax.com/objects/230/ˇYou can use the sprintf method to create a string with numeric conversions, etc... then use strsize on the resulting formatted string. It is somewhat expensive in memory terms, but very useful in the right hands.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve


Propalyzer: Propeller PC Logic Analyzer (http://www.brouhaha.com/~sdenson/Propalyzer)
http://forums.parallax.com/showthread.php?p=788230 (http://forums.parallax.com/showthread.php?p=788230)