How do you convert a String to a float or int?

后端 未结 5 1605
醉酒成梦
醉酒成梦 2021-02-19 03:56

In an Arduino program I\'m working on the GPS sends the coordinates to the arduino through USB. Because of this, the incoming coordinates are stored as Strings. Is there any way

5条回答
  •  遥遥无期
    2021-02-19 04:29

    You can get an int from a String by just calling toInt on the String object (e.g. curLongitude.toInt()).

    If you want a float, you can use atof in conjunction with the toCharArray method:

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

提交回复
热议问题