Help with ASCII and numbers
average joe
Posts: 795
I'm working on a object and I need to:
Receive three numbers as an ASCII encoded string, space delimited and terminated with CR + LF, with FullDuplexSerial.
Then I would like to convert to 3 - 8 bit numbers, pack those into a long and pass the long as result.
So, I'm expecting to receive :
"0 0 0" as ASCII or as decimal "48, 32, 48, 32, 48, 13, 10"
to
"255 255 255" or as decimal "50, 53, 53, 32, 50, 53, 53, 32, 50, 53, 53, 13, 10"
I'm trying to figure out an easy way to do this and keep drawing a blank. I started working on it and got this far :
Receive three numbers as an ASCII encoded string, space delimited and terminated with CR + LF, with FullDuplexSerial.
Then I would like to convert to 3 - 8 bit numbers, pack those into a long and pass the long as result.
So, I'm expecting to receive :
"0 0 0" as ASCII or as decimal "48, 32, 48, 32, 48, 13, 10"
to
"255 255 255" or as decimal "50, 53, 53, 32, 50, 53, 53, 32, 50, 53, 53, 13, 10"
I'm trying to figure out an easy way to do this and keep drawing a blank. I started working on it and got this far :
index := 0 tmp := serial.rx repeat while tmp <> -1 buff[index ++] := tmp tmp := serial.rx index := 0 repeat while buff[index + 1] <> 10 repeat while buff[index] <> 13 places := 0 repeat while buff[index] <> 32 temp[places ++] := buff[index ++] - 48 if places == 2 tmp1 := ((temp[2] * 100) + (temp[1] * 10) + temp[0]) elseif places ==1 tmp1 := ((temp[1] * 10) + temp[0]) else tmp1 := temp[0]Any help would be greatly appreciated!
Comments
1980's MBASIC was/is one of those languages that had terrible structure, in terms of GOTO and line numbers, but at the same time, string handling that IMHO is still way ahead of Spin.
in basic
and that returns a string, the string has a leading space if it is positive or a - sign if negative. In MBASIC you didn't even need to declare A$ if you didn't want to - it was there ready to use with just one line of code and no "extra" supporting code like having to declare arrays (like in Spin). Though as MBASIC evolved into the MBASIC compiler, QBASIC, and then Visual Basic and VB.Net, it ended up borrowing more and more good things from C like the necessity of declaring things before you use them.
if you want you can then use TRIM to remove any leading spaces.
Kuroneko's code does the job better. And with way less supporting code (especially all the string routines you will never use).
Thanks again kuroneko. You've helped me several times.
I'll look through the string functions again, but not sure they will help right now. A+ for the effort though Doc!
I *think* there are enough functions in Kye's modified code to replicate this in something similar to Basic. eg mid$() becomes str.mid() It is one line of code in Basic, where A$ is the input string "0 0 0" and B$ is the output string
I'm kind of motivated to take another look at some of the versions of Basic that let you do this sort of thing. There was a version of basic that got translated into C but it was C++, not C89 and it took ages to code all the special differences between C89 and C++. But with the new GCC it might be a much simpler proposition. Strings should not be something you have to think about too much - they should be easy to work with.
*edit*
I TOTALLY agree that strings should not take a lot of thought. Now that I have 0-terminated strings, I could probably hook into numbers. Still have more work to do first.
Another thing to look at, ParallaxSerialTerminal is essentially FullDuplexSerial with a lot of added Spin functions for things like string input and numeric conversion.