如何将String转换为float或int?

Xjk*_*3vk 15 arduino

在Arduino程序中,我正在研究GPS,通过USB将坐标发送到arduino.因此,传入的坐标存储为字符串.有没有办法将GPS坐标转换为float或int?

我试过 int gpslong = atoi(curLongitude)float gpslong = atof(curLongitude),但他们都引起Arduino的给出错误信息:

error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
Run Code Online (Sandbox Code Playgroud)

有没有人有什么建议?

nne*_*neo 23

你可以得到一个intString由只调用toInt了对String对象(例如curLongitude.toInt()).

如果你想要一个float,你可以atof结合使用toCharArray方法:

char floatbuf[32]; // make this at least big enough for the whole string
curLongitude.toCharArray(floatbuf, sizeof(floatbuf));
float f = atof(floatbuf);
Run Code Online (Sandbox Code Playgroud)