String object to int

This is an old question that seems it doesn't want to disappear, but after all and reading old posts and google around it, I've not found the solution :-( I try to do a simple task like convert one String object to int (or float).

  int length = values[0].length();
  char time[length+1];
  values[0].toCharArray(time, sizeof(time));
  int t = atoi(time);
  Serial.println(t);
  Serial.println(time);

If values[0] has 227759 value... when I execute the code before, I receive the next result:

31151
227759

The char time is correct, but fails miserably the atoi() conversion... What am I doing wrong? It turns me mad!

Thanks for advance! Xavier

It may not be obvious, but you can't fit

227759

into an int.

Try a "long" and "atol"

ints can contain values from -32,768 to 32,767. Anything outside that and you get overflow. You'll need to use a long to contain larger values.

Epic fail!!!!

It was obvious!

double t = atof(time);

Thanks!!

Epic fail!!!!

Is right.

double t = atof(time);

The time string does not contain a float value. It contains an unsigned long!

Ok, but wha'ts the right function? is there any atod? :roll_eyes:

Pssst, up there ^^^ reply #1

Double epic fail! :blush: