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?

Ana
  • 480
  • 3
  • 7
  • 13

2 Answers2

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().

Nick Gammon
  • 38,901
  • 13
  • 69
  • 125
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.

SimonM
  • 11
  • 1