Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
Parsing strings with F401RE
Hello all;
First of all thank you who spend their valuable time to answer my (may be simple, foolish) questions before... Great support you provide here...
Now, I have some other questions following. I could solve easily this parsing problems with Arduino platfor with specific functions. I need some guidance, support for mbed...
1. I got a Rx string which stored in let say;
char buffer[20] =""
The string (data) from the sensor can be;
75 23.5 1235 or 12 24 237 or 9 9 43 etc
As you can see these numbers in string are space delimited and in different digit numbers.
I want to store them to different variables as integer or float... Any suggestions??
2. I want to parse the a string like
XXXX:12,23,456,12367,12
Similarly digits can be different but in these case all is integer. All numbers will be stored in different variables to compare or store ....
P.S. Is it a good choice to use std::string http://www.cplusplus.com/reference/string/string/ ? Timing is not important. The system can tolerate many milliseconds...
Thanks
2 Answers
8 years, 10 months ago.
Generally going for real strings is a bad idea on an MCU, that said the F401 has quite some resources, and if you don't need them anyway, you could use it.
However this seems all pretty straightforward stuff which works fine with char arrays. Have a look at for example: http://www.cplusplus.com/reference/cstdio/sscanf/. You can also split it first into seperate parts: http://www.cplusplus.com/reference/cstring/strtok/ together with http://www.cplusplus.com/reference/cstdlib/atoi/ and http://www.cplusplus.com/reference/cstdlib/atof/
8 years, 10 months ago.
I shall offend a lot of people, because I like C++ strings and vectors when the MCU has enough RAM.
Look at the "tokenizer" here :http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html, I use this code very often.
I hope this can help you.