gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:18:21 25 4
gpt4 key购买 nike

在我正在处理的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com