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.

1
  • 3
    If you have std::string, chances are you also have std::stoi
    – Mat
    Oct 10 at 17:23
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.