7

There is String.toInt(), but no String.toLong() or the many other variations. Do I have to resort to atol(String.c_str()) or is there a better way to convert a String to a long?

asked Mar 12, 2016 at 2:43

2 Answers 2

8

Using atol(String.c_str()) looks OK to me. If there was a String.toLong() it would be written that way anyway.

In fact, looking at the code for String.toInt() that's exactly what it does:

long String::toInt(void) const
{
 if (buffer) return atol(buffer);
 return 0;
}

So the answer is: use String.toInt().

answered Mar 12, 2016 at 5:00
1
  • 1
    Interesting. I wonder why they didn't just call it toLong()?? Commented Aug 24, 2017 at 0:20
1

So the answer is: use String.toInt().

Probably not, as this is the definition of a String method:

long String::toInt(void) const

The real work is:

atol(buffer);

ASCII to long.

answered Apr 11, 2021 at 10:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.