String from serial

Hey, I'm trying to get the Arduino to take in a 6-digit numerical string from a processing sketch. Since the Arduino reads serial one character at a time, what would be the best way to assemble the 6 characters into a single integer variable?

Thanks

The easiest to understand is to wait until all 6 characters have arrived, then read them, and store them in an array.

int index;
char inData[8]; // Or however many...

void loop()
{
   if(Serial.available() >= 6)
   {
      index = 0;
      for(byte i=0; i<6; i++)
      {
         inData[index] = Serial.read();
         index++;
         inData[index] = '\0'; // Keep string NULL terminated
      }

      // Use inData for whatever; use atoi(inData) to convert to int
   }
}

and then use atoi() to get them into an integer.

Does it really look like this,int myArray[7] = {0, 1, 2, 3, 4, 5}; ?

Or is it more like thisint myArray[7] = {'0', '1', '2', '3', '4', '5'}; ?

If the former, then simply multiply each array element by the appropriate power of ten, and sum the values.
Watch out for values over 32767.

Sorry about deleting the post, I thought I had it for a second. The array is not being manually defined, it's being defined one digit at a time from the loop shown in the second post of this thread.

Here's an example of what I'm having a problem with:

char myArray[7] = {'0', '1', '2', '3', '4', '5'}; 
void setup () {
  Serial.begin(9600);
}
void loop () {
  Serial.println(atoi(myArray)); 
}

This outputs "12345" which is fine, that will work perfectly for what I need. The problem is that the highest end of the range of the number is over 100000, so say the number I'm trying to read is "123456", if I try:

char myArray[7] = {'1', '2', '3', '4', '5', '6'}; 
void setup () {
  Serial.begin(9600);
}
void loop () {
  Serial.println(atoi(myArray)); 
}

In this example, rather than getting "123456", I'm getting "-7616". I can't for the life of me understand where this value is coming from unless "atoi()" has some kind of limit.

an "int" has a range -32768 to +32767.

That makes sense that that would have been over the limit for int, so I tried using "long" instead of "int". Even though I'm well within the range for a "long" variable, I'm still getting the "-7616", any ideas?

char myArray[7] = {'1', '2', '3', '4', '5', '6'};
void setup () {
  Serial.begin(9600);
}
void loop () {
  long num = atoi(myArray);
  Serial.println(num);
}

EDIT: I was starting to fry my brain trying to figure this out, then it hit me, if "atoi()" stands for "ASCII to Int", what would happen if I changed the "i" to an "l" for long, low and behold it worked.

Be very careful - your array is only working here because the compiler is filling unused elements with zero.
In a running system, this advantage may disappear, as string lengths change.

Btw. Just to be sure, I would explicitly terminate the string:

char myArray[7] = {'1', '2', '3', '4', '5', '6', '\0'};

or

char myArray[] = "123456";

Sometimes the compiler does the right thing implicitly, but I always prefer to see explicitly what's going on.

Well, like I said, I wasn't defining the array manually, the array is being created by reading serial one character at a time. I only posted the "{'1', '2', '3'}" example to show what the array contains, not how it's created. The "atol()" function seems to be working well for what I need.

I wasn't defining the array manually, the array is being created by reading serial one character at a time.

So that you don't waste people's time, it is best for everyone if you post the code that you're actually using.
Passing unterminated strings to library routines is a common noob bug.