# incomprehensible conversion

in the attached program that does not perform the conversion properly and I no longer know where to look , can you help me? The program converts a string to its decimal value

en el programa que adjunto, no me realiza la conversion correctamente y ya no se por donde mirar, ¿podeis ayudarme? El programa convierte una cadena de caracteres a su valor decimal

void setup() { // put your setup code here, to run once: Serial.begin(9600); }

void loop() { // put your main code here, to run repeatedly: Serial.print("cadena de prueba "); Serial.println(convInt("9999",4)); do{} while (true); } int convInt(char entrada, int longcad){ int salida=0; double temporal=0; double t2=0; for (int i=longcad-1; i>-1; i--){ double pot=pow(10,((longcad-1)-i)); Serial.print("pot="); Serial.println(pot); t2=(int(entrada)-48);* _ Serial.print("pot*t2= ");_ _ Serial.println(pot*t2);_ _ temporal=temporal+(pot*t2);_ * salida= int(temporal);* * Serial.print("temporal= ");* * Serial.println(int(temporal)); }* * return salida;* } the output is 9998

Do not use the pow() function. Besides, you don't need exponentiation for this.

Example: "9999" (0 * 10) + 9 = 9 (9 * 10) + 9 = 99 (99 * 10) + 9 = 999 (999 * 10) + 9 = 9999

Another example: "1234" (0 * 10) + 1 = 1 (1 * 10) + 2 = 12 (12 * 10) + 3 = 123 (123 * 10) + 4 = 1234

``````int convInt(char *entrada, int longcad){
int salida=0;
for (int i=0; i``````
``` Better still, read the documentation and use the atoi function. See point 13 on the sticky post.[/code] ```
``` ```
``` josejmalco June 28, 2016, 4:33am #5 Thank you very much everyone for your answers. At the end the solution has been the atoi function , was the first I've tried and it worked . Thank you Muchas gracias a todos por vuestras respuestas. Al final la solucion ha sido la funcion atoi, ha sido la primera que he probado y ha funcionado. Gracias system closed May 5, 2021, 9:57pm #6 ```
``` Home Categories FAQ/Guidelines Terms of Service Privacy Policy Powered by Discourse, best viewed with JavaScript enabled ```