problem in reading String and performing Mathematical operations....

i want to add the values inside a,b,c and then want to print them on serial port.But the Serial.println command is printing the Decimal value of the addition( sum=a+b+c )that is 150 but i want output should be 6.Need Help Please Reply...Thanks My Code is:-


int a,b,c,sum;
void setup() 

void loop() 
 String reportString = "1$123$456$$789";
 int spacePosition0 = reportString.indexOf('1');
 int spacePosition1 = reportString.indexOf('

[/code][/code]); a=(int)reportString.charAt(spacePosition1 + 1); b=(int)reportString.charAt(spacePosition1 + 2); c=(int)reportString.charAt(spacePosition1 + 3);

if (reportString.charAt(spacePosition0) == '1') {     sum=a+b+c;     Serial.println(sum);     delay(1000); } } ```


The calculation is correct - you’re adding the ASCII values of the characters.

@AWOL i want Serial.Println(sum); to print 6 as the sum of a=1,b=2,c=3.But the command is printing 150 by adding the Decimal Values of a,b and c (49,50 and 51) respectively.So AWOL how can i get this value.......please help.

Subtract '0' from each item before summing them.

Thanks AWOL.It was a great help from Your side.

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).