invalid cast from type 'string' to type 'int'

I have spent 6 hours with the error: invalid cast from type 'string' to type 'int'. I am trying to convert a string to a long. Mycode: recordeddataR = long(values[1]); I think I need to convert my values[1] to a char string and use "atol" but I don't know how. I tried: recordeddataR = long(toCharArray(values[1],10)); That gave me: 'toCharArray' was not declared in this scope.

I wish strings in C were just another variable type like strings in Basic. I just want to get my robot working.

I would appreciate any help. Thanks.

Post code.
Post actual error messages

Some background: I am transmitting two longs from a VB program (like 1234,-567890) to an Arduino MEGA. I had to read the serial into the Arduino at one character at a time and then concatenate the characters into a two strings. Now I want to convert these two strings into longs (like 1234 and -567890). Its a real pain. Surly there is a better method?

void setup() {
 Serial.begin(115200);
 char* behind;
 long lValue = strtol("123456789understood?", &behind, 10);
 Serial.print("Result: ");
 Serial.print(lValue, 10);
 Serial.print(" unprocessed rest: ");
 Serial.println(behind);
}
void loop() {}
Result: 123456789 unprocessed rest: understood?

My declairs:

String values[2]={" ", " "};
char ch = ' ';
int fieldIndex = 0;
long recordeddataL;
long recordeddataR;
recordeddataR = long(values[1]);

gives the following error:

invalid cast from type 'String' to type 'long int'

recordeddataL = long(toCharArray(values[1],10));

gives me the following error:

'toCharArray' was not declared in this scope

Thanks Whandall. Was that an answer?

Casting is re-interpretation, not conversion.

For conversions there are a bunch of different routines, strtol() for example.

Its name is the short form of String-To-Long.

strtoul(), ....

Thanks Whandall. I tried: recordeddataR = strtol(values[1]);but I got this error for that line of code:

cannot convert 'String' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'

Don't use Strings, or if you must, you can turn them into a char array, and then use strtol.

Sorry if I jumped ahead and lost you.

So in your special case it would be

char* behind;

recordeddataR = strtol(values[1].c_str(), &behind, 10);
void setup() {
  Serial.begin(115200);
  int base = 10; // decimal this time
  String toDecode = "123456789understood?"; // string to convert

  char* behind; // will point behind the number
  
  long longVal = strtol(toDecode.c_str(), &behind, base);

  Serial.print("Result ");
  Serial.print(longVal, base);
  Serial.print(" non-numeric rest '");
  Serial.print(behind);
  Serial.println("'");
}
void loop() {}

GOTIT! Thanks to davekw7x at http://forum.arduino.cc/index.php?topic=44922.0

          recordeddataL = stringToLong(values[0]);
          recordeddataR = stringToLong(values[1]);
long stringToLong(String s)
{
   char arr[12];
   s.toCharArray(arr, sizeof(arr));
   return atol(arr);
}

Try to parse two longs out of one String with that method. ;)

The strtol routine is very versatile, have a look Atmel-Doc strtol().

To show you how easy the routine can be used:

void setup() {
  Serial.begin(115200);
  int base = 10; // decimal this time
  char* behind; // will point behind the number

  String toDecode = "1234,56789understood?"; // string to convert

  long longVal1 = strtol(toDecode.c_str(), &behind, base);
  long longVal2 = strtol(++behind, &behind, base);

  Serial.print("Result ");
  Serial.print(longVal1, base);
  Serial.print(" and ");
  Serial.print(longVal2, base);
  Serial.print(" non-numeric rest '");
  Serial.print(behind);
  Serial.println("'");
}
void loop() {}
Result 1234 and 56789 non-numeric rest 'understood?'