arduino - 如何将字符串转换为 float 或整数?

标签 arduino

在我正在处理的 Arduino 程序中,GPS 通过 USB 将坐标发送到 arduino。因此,传入的坐标存储为字符串。有没有办法将 GPS 坐标转换为浮点数或整数?

我试过 int gpslong = atoi(curLongitude)float gpslong = atof(curLongitude) ,但它们都导致 Arduino 出现错误:

error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'

有没有人有什么建议?

最佳答案

您可以获得 int来自 String只需调用 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);

关于arduino - 如何将字符串转换为 float 或整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200035/

相关文章:

c++ - 什么是 MaxDetect 1-wire 总线?

sql - 我可以在 SQL Server 上进行实时查询吗?

c++ - 在不同的机器上编译会产生不同的 Arduino ESP8266 项目的 hex 文件

c++ - Qt 读取串行数据 - 工作代码但需要更可靠

c - 如何在c中从char*访问char[]?

c++ - 如何将十六进制转换为基数为10的字节

c - 在C中以微秒为单位制作定时循环

c++ - 为 Arduino 编写自定义类时出现错误 'does not name type'

澄清在 Arduino 中使用结构和在 PROGMEM 中存储结构

java - 制作 USB 设备,用 Java 控制它