Go Down

Topic: Problem about convert array of string to char using toCharArray (Read 2165 times) previous topic - next topic

chenni1

My code only convert the fraction part of string to char. For example, the string is 123.8724; i get 0.8724
My code is:

  if (inbyte == '!'){  // Handle delimiter, ! IS MY endline character.
    for (int i =0;i< counter;i++)
    {   
          data.toCharArray(carray, 12); //put readStringinto an array
          float f = atof(carray + 1); //convert the array into an Integer
          desiredT = f;
          Serial.println(desiredT,5);
             
    }
    inbyte = 0;
  }
}

pYro_65

#1
Apr 01, 2014, 05:06 am Last Edit: Apr 01, 2014, 05:19 am by pYro_65 Reason: 1
If you are using the String library you can use standard array logic to access the pointer rather than toCharArray which copies the entire array.

Code: [Select]

void setup() {
 String data = "123.456";
 char * carray = &data[ 0 ];
 float f = atof( carray  );
 Serial.begin( 9600 );
 Serial.print( f, 5 );
}

void loop() {}


The alternative using toCharArray works like this: ( carray must be an array, or pointer to valid data )
Code: [Select]
void setup() {
 String data = "123.456";
 
 char carray[ 10 ];
 
 data.toCharArray( carray, 10 );
 
 float f = atof( carray  );
 Serial.begin( 9600 );
 Serial.print( f, 5 );
}

void loop() {}
Forum Mod anyone?
https://arduino.land/Moduino/

427v8


My code only convert the fraction part of string to char. For example, the string is 123.8724; i get 0.8724
My code is:

  if (inbyte == '!'){  // Handle delimiter, ! IS MY endline character.
    for (int i =0;i< counter;i++)
    {   
          data.toCharArray(carray, 12); //put readStringinto an array
          float f = atof(carray + 1); //convert the array into an Integer
          desiredT = f;
          Serial.println(desiredT,5);
             
    }
    inbyte = 0;
  }
}



Code: [Select]

if (inbyte == '!')
{
   /// you get your string into a null terminated array somehow....

    char *dot = strstr(carray, ".");
    Serial.println(dot);
}


Go Up