Question about embedding variable in a string statement
Don M
Posts: 1,652
I could swear I have seen some tricky way of embedding a variable in a string statement but can't find an example of it.
What I'm wondering is can I structure this statement in a way to make it work?
Let's say I have a byte variable called "line".
I want to print a statement like this: "Stopped at line *decimal value of line here* before continuing"
term.str(string("Stopped at line " ? "before continuing"))
Thanks.
What I'm wondering is can I structure this statement in a way to make it work?
Let's say I have a byte variable called "line".
I want to print a statement like this: "Stopped at line *decimal value of line here* before continuing"
term.str(string("Stopped at line " ? "before continuing"))
Thanks.
Comments
...prints "Value: 3" To do a multi-byte decimal value you would need a version of the "dec" function found in most video I/O objects (where it doesn't belong anyway) modified to write to RAM instead of spitting the bytes out to an interface. You can find one of those in my PropCMD object in the obex.
Of am I missing something in what you want to do?
I'm pretty sure someone has an object that lets you output strings with embedded variables but I don't recall what it's called and I've never tried it. I just remembering someone saying they can't believe people resort to doing what I just posted.
If you want to do it the way localroger suggests, I'm pretty sure I have a dec method that writes the ASCII characters of a value to a location in RAM. If you'd like to do it that way let me know and I'll look for the method. If you're output a ASCII characters to RAM, it usually makes things easier if you can have the number be a fixed length. Hex is more conducive to this kind of manipulation since we're used to seeing hexadecimal values with leading zero.
I believe there might be an object in the library to do this. "Numbers" maybe?
Duane- yes I know I can do that and usually do. It's just that I thought I saw somewhere on here an example of something similar but all in the same line.
I think they must have used a special formatting object to do it then. Maybe someone else will remember more details.
Here is one of the objects Mike G. uses in his HTTPServer app. This object does exactly what you're asking and a couple other nice things.
http://code.google.com/p/spinneret-web-server/source/browse/trunk/MultiSocketServer_MikeG/StringMethods.spin
Once I start this object, I use a simple method to work with buffers.
I'd imagine there are some much fancier ways to do this, but I really do like Mike G's object. It's spifforiffic.
I haven't tried this but why not?
That's only good if the variable value is between 0 and 9 (inclusive). Plus it doesn't meet the "single line of code" requirement.
WHAT single line of code requirement???
See post #4.
Sorry, blame Don.
Sorry. Yeah, I was responding to LocalRangers idea but trying to insert a variable
rather than a literal.
DON! It's YOUR fault! (Duane told me to say that )