Problem with char array to int

Hi,

for my actually project, I need to split a char array.
The array looks like this:

char array[] = {PWMS45F120B180$};

First, the program check, if array[0] and 1 and 2 is PWM, then it writes the numbers in other char arrays.

servo[] = {45};
motor_control[] = {120};
bow_thrust[] = {180};

The program:

#include "WProgram.h"
#include "SerialEncode.h"

SerialEncode::SerialEncode() {}

/************************/
/*encode_pwm            */
/*first three chars PWM */
/*S: Servo              */
/*F: Motor Controller   */
/*B: Bow Thruster       */
/*$: end char           */
/************************/
void SerialEncode::encode_pwm(char encode[],int *se,int *mc,int *bt)
{
    int i=4;
    char servo[3];
    char motor_control[3];
    char bow_thruster[3];
    int e=0;
    while (encode[i]!='F')
    {
        servo[e] = encode[i];
        e++;
        i++;
    }
    e=0;
    while (encode[i]!='B')
    {
        motor_control[e] = encode[i];
        e++;
        i++;
    }
    e=0;
    while (encode[i]!='

And the error messages:

Lines 41,42,43 the char can not be converted to const char*

What is wrong?

Philipp)
   {
       bow_thruster[e] = encode[i];
       e++;
       i++;
   }
   *se = atoi(&servo);
   *mc = atoi(&motor_control);
   *bt = atoi(&bow_thruster);
}


And the error messages:

§DISCOURSE_HOISTED_CODE_3§


What is wrong?

Philipp

Why did you add the & in front of servo, motor_control, and bow_thruster? The & is not needed.

The atoi function takes as input a NULL-terminated array of characters which it does not modify, which is what servo, motor_control, and bow_thruster are (or could be, if they are NULL terminated).

Ok,
but I wrote a new function now. And I have still problems. At the end of the function, I convert the servo, motor controller, and the bow_thrust arrays to an Integer. But I get wrong numbers at the end. When I only convert one of these arrays to an integer the number is right. Then I tried to print the numbers with the Serial Function. When I only print on of these arrays, I get the right number but when I print all arrays, I get wrong numbers.

void SerialEncode::encode_pwm(char encode[],int *se_,int *mc_,int *bt_)
{
    char actuel_char;
    char se[3];
    char mc[3];
    char bt[3];
    int i=3;
    int e=0;
    while (encode[i]!='

When I clear the slashes in front of the three Serial.println, I get this ouput:

12345612
45612
12

When I clear only the slashes in front of one serial.println, I get the right number:

123
or
456
or
12

What is wrong?

Philipp)
   {
       if (encode[i]=='S')
       {
           actuel_char = encode[i];
           e=0;
       }
       else if (encode[i]=='F')
       {
           actuel_char = encode[i];
           e=0;
       }
       else if (encode[i]=='B')
       {
           actuel_char = encode[i];
           e=0;
       }
       else
       {
           if (actuel_char=='S')
           {
               se[e] = encode[i];
               e++;
           }
           else if (actuel_char=='F')
           {
               mc[e] = encode[i];
               e++;
           }
           else if (actuel_char=='B')
           {
               bt[e] = encode[i];
               e++;
           }
       }
       i++;
   }
   //Serial.println(se);
   //Serial.println(mc);
   //Serial.println(bt);
   //*se_ = atoi(se);
   //*mc_ = atoi(mc);
   //*bt_ = atoi(bt);
}


When I clear the slashes in front of the three Serial.println, I get this ouput:

§DISCOURSE_HOISTED_CODE_1§


When I clear only the slashes in front of one serial.println, I get the right number:

§DISCOURSE_HOISTED_CODE_2§


What is wrong?

Philipp

I'm not seeing where you're terminating your strings.

            if (actuel_char=='S')
            {
                se[e] = encode[i];
                e++;
                    [glow]se[e] = '\0';[/glow]
            }
            else if (actuel_char=='F')
            {
                mc[e] = encode[i];
                e++;
                    [glow]mc[e] = '\0';[/glow]
            }
            else if (actuel_char=='B')
            {
                bt[e] = encode[i];
                e++;
                    [glow]bt[e] = '\0';[/glow]
            }

What is wrong?

You must make the array sizes large enough to hold all of the digit chars and also the terminating '\0' byte.

If you know (for absolutely damnsure) that there are a max of three decimal digits in the integers:

    char se[4];
    char mc[4];
    char bt[4];

In general, with 16-bit ints you can have as many as 5 decimal digits, so maybe the dimensions of the arrays should be 6???