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.
11 years, 6 months ago.
Convert a float number to string?
include the mbed library with this snippet
#include "mbed.h" #include <string> using std::string; string data; float temperature = 20.43; data.append("SHT15 |C:"); data = data + to_string(temperature); //error identifier "to_string" is undefined data.append("|F:"); data = data + to_string(this->get_Temperature_F()); //error
I try convert data from sensor to data type STRING. But this method convert does not work.
The compiler report error: identifier "to_string" is undefined. Why???
Example that I used is here: http://www.cplusplus.com/reference/string/to_string/
Thank you for your advice.
3 Answers
11 years, 6 months ago.
See the little warning message at the top of the page you link, that is only available in C++11, which isn't used here.
While you have to be a bit more careful, but in general working with (s)printf and char arrays is alot lighter on memory than using strings.
11 years, 6 months ago.
Hi Mark,
maybe you should try the sprintf function. I compile the code using mbed it seems to work but I did not try it on a hardware platform. Your number converted as a string should be under the buffer variable.
Check out http://www.cplusplus.com/reference/cstdio/sprintf/
#include "mbed.h" #include "stdio.h" DigitalOut myled(LED1); int main() { char buffer[50]; float c = 3.5; n=sprintf (buffer, "%f", c); while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }