1

Pardon me, I am a novice programmer. I was trying to use the toInt() function for a std::string but I noticed that toInt() only works for the String class. So, I have an std::string sliderValue and wanted to convert it to the String class so I could use the toInt() function. Here, I found a way that seemed to pacify Intellisense, vMicro, and building. I did:

String convertValue = strcpy(new char[sliderValue.length() + 1], sliderValue.c_str());

so I could do:

ledcWrite(ledChannel, convertValue.toInt()  * (10.24));

Is there a better way to do this or am I on point? Also, I'd love to get insight in what I'm doing, if possible. Thanks folks!

P.S. I looked up this here at arduino stackexchange, and it didn't seem to provide a solution specific to my question.

Enna
  • 13
  • 4

1 Answers1

1

There is absolutely no need to use String (for anything, generally...). Instead you can either (if you have it) use std::stoi() or use strtol() on the c_str of the string.

ledcWrite(ledChannel, std::stoi(sliderValue)  * (10.24));

Or

ledcWrite(ledChannel, strtol(sliderValue.c_str(), NULL, 10)  * (10.24));

The former just converts into the latter anyway.

Majenko
  • 105,851
  • 5
  • 82
  • 139