Pages: [1]   Go Down
Author Topic: Pasar de String a int,float,long...[Solucionado]  (Read 1037 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Buenas, tengo un proyecto en el que recibo a traves de un socket una cadena de caracteres y llegado un punto necesito convertir esa cadena en un numero, da igual de que tipo sea.

Ya he probado con atoi y atol, incluyendo la libreria <stdlib.h> pero me da el siguiente error:

 cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'



gracias
« Last Edit: July 18, 2013, 10:41:08 am by jorge_hura » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has probado con serial.parseint(), copio y pego lo que pone en la referencia de la pagina oficial de arduino;

Serial.parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped. Serial.parseInt() is terminated by the first character that is not a digit.
Serial.parseInt() inherits from the Stream utility class.

esta opción esta disponible a partir de IDE 1.0.

un saludo
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


He encontrado una forma de hacerlo manualmente, pero gracias.

Dejo aqui la solucion por si le sirve a otro.

String tiempo[]={"0.","1.","2.","3.","4.","5.","6.","7.","8.","9.","10.","11.","12.","13.","14.","15.","16.","17.","18.","19.","20.","21.","22.","23.","24.","25.","26.","27.","28.","29.","30.","31.","32.","33.","34.","35.","36.","37.","38.","39.","40.","41.","42.","43.","44.","45.","46.","47.","48.","49.","50.","51.","52.","53.","54.","55.","56.","57.","58.","59."};


for(i=0;i<60;i++)
     {
       if(tiempo == probando)// probando es la variable que recibo a traves de socket y que contiene el string a cambiar
       {
         if(aux ==1)
         {
         NuevaHora=i;
         Serial.print("La nueva hora introducida es");
         Serial.println(NuevaHora);
         aux=2;
         }
         else if(aux=2)
         {
           NuevoMinuto=i;
           Serial.print("La nueva hora introducida es");
           Serial.println(NuevoMinuto);
           aux=0;
         }
       }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Aunque ya lo tienes solucionado, aquí te dejo cómo lo he arreglado yo:

long string2long(String string){
  char cadena[string.length()+1];
  string.toCharArray(cadena, string.length()+1);
 
  return atol(cadena);
}

Puedes modificarlo fácilmente para que pase a int, float o lo que quieras.

Saludos,
Manolo
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tengo una duda  en donde puedo conseguir la libreria  stdlib.h 
Logged

Pages: [1]   Go Up
Jump to: