Strange behaviour when trying to convert string to int

Hello everyone,
I'm Italian and new to this forum so I apologize if I make any mistake.

My question is, looking at my code, why the converted string to integer is missing as many units as the number of loops made after the second cipher?

The fact strange to my eyes is that with two ciphers everything is fine.

Can someone help me?
Am I doing wrong something?
I am using an Arduino Mega 2560 R3

char Array[100]={"W,1,12345,somethingelse"};

// I want to extract "12345" and convert it into an integer
// starts at Array[4], ends to Array[4+5] where 5 is  its lenght

void setup(){
   Serial.begin(9600);
   
   long number = stringToInt(Array, 4, 5);

   Serial.println();
   Serial.println(number);
}

void loop(){

}
//scan from right to left, starting from startIndex ending when reached lenght
long stringToInt(char* a, int startIndex, int lenght){ 
   long result=0;
   int i=0;
   int e=0;
   for(i=startIndex+lenght-1;  i>=startIndex; i--){
      Serial.println(pow(10,e));
      result = result + (a[i]-'0')*(pow(10,e++));
      Serial.println(result);
   }
   return result;
}


//SERIAL OUTPUT
/*
   1.00
   5
   10.00
   45
   100.00
   344
   1000.00
   2343
   10000.00
   12342

   12342
*/

But if I use this code to manipulate the string, everything goes well

number = (Array[4]-'0')*10000 + (Array[5]-'0')*1000 + (Array[6]-'0')*100 + (Array[7]-'0')*10 +(Array[8]-'0');

stringToInt is for use on String objects (as opposed to character arrays.) Might be worthwhile trying your code again with:

String Array={"W,1,12345,somethingelse"};

Blackfin:
stringToInt is for use on String objects (as opposed to character arrays.)

Hum... stringToInt is OP’s own code available in the posted code....

The issue is possibly an approximation due to the use of double computation versus int computation and rounding down

Try with

long stringToInt(const char* a, const int startIndex, const int length)
{ 
   long result=0, e=1L;
   for(int i=startIndex+length-1; i>=startIndex; i--){
      result = result + ((long) (a[i]-'0'))*e; // (long) cast is unnecessary but for “coherence”
      e *=10L;
      Serial.println(result);
   }
   return result;
}

or that one going in the other direction

long stringToInt(const char* a, const int startIndex, const int length)
{ 
   long result=0;
   for(int i=startIndex; i<startIndex+length; i++){
      result = 10*result + ((long) (a[i]-'0')); // (long) cast is unnecessary but for “coherence”
      Serial.println(result);
   }
   return result;
}

The serial print out of pow(10,e) is defaulting to 2 dp and rounding, and is fooling you into thinking it is provide the exact integer power of 10. pow() returns a float which can be very close, but is not likely to be what you expect.

Change your print statement to see more of what is actually happening

Serial.println(pow(10,e),4);

You can certainly extract the numbers like you have with the adding of the powers times the digits like you have done here

number = (Array[4]-'0')*10000 + (Array[5]-'0')*1000 + (Array[6]-'0')*100 + (Array[7]-'0')*10 +(Array[8]-'0');

Unfortunately you will not always get the right result with what you have used, because you can overflow the value of an integer in the ten thousands place. Use 10000L to let the compiler know what to expect. See the notes and warnings here

J-M-L:
Hum... stringToInt is OP’s own code available in the posted code....

Oops.

I saw

https://www.arduino.cc/en/Tutorial/StringToInt

and leapt before I looked (or read...)

As you were...

Yeah you were quick to conclusion as clearly this was a function call and not a method call (there was no instance or class to call that on)

Time to go to bed :slight_smile:

Thank you all for your quick response!!!
Especially for giving me the accurate explainations of why it behaved like that!