作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我正在处理的 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/
我是一名优秀的程序员,十分优秀!