#include <cstring>
...
string Str_es = "0123";
...
____________________________
Come faccio a convertire la stringa in int in maniera da poter fare i relativi calcoli con numero 123 (in questo caso)?
#include <cstring>
...
string Str_es = "0123";
...
____________________________
Come faccio a convertire la stringa in int in maniera da poter fare i relativi calcoli con numero 123 (in questo caso)?
Così dovrebbe funzionare!!codice:#include<string.h> int str2int(char *s) { int intVal=0; bool neg = false; for (unsigned int i=0; i< strlen(s); i++) { if (s[i]=='-') neg= ! neg; else intVal= (intVal*10)+(s[i]-48); } return (neg)? -intVal: intVal; }
Questa funzione va bene passando dei delle stringhe tipo "char *" ma non string.Originariamente inviato da Grunt
Così dovrebbe funzionare!!codice:#include<string.h> int str2int(char *s) { int intVal=0; bool neg = false; for (unsigned int i=0; i< strlen(s); i++) { if (s[i]=='-') neg= ! neg; else intVal= (intVal*10)+(s[i]-48); } return (neg)? -intVal: intVal; }
Io ho un array di stringhe di tipo "string[]" e 4 caratteri di queste stringhe (sempre nella stessa posizione) li devo convertire ad int.:master:
la classe string (che sta in string e non in cstring) ha un metodo che ritorna la stringa in stile char* chiamato c_str. Puoi utilizzare quello.
bye
There are 10 kinds of people in the world: who knows the binary numeration and who not