I'm trying to process a string to get the numerical values from the string.
The functions toInt(), toFloat() or toDouble() are giving completely wrong values for the conversion.
String temp;
int tmp[20];
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
String value = "$20$(-3.7001624954631467 139.85888350863482),(48.1492194613472 18.3989561222478),(-46.42921396602168 -45.14057884500802),(60.61327990751049 -117.32494850168642)@4";
Serial.println(value);
tmp[0] = value.indexOf("$");
tmp[1] = value.indexOf("$", tmp[0]+1 );
temp = value.substring(tmp[0]+1 , tmp[1]);
uint8_t command_code = temp.toInt();
tmp[2] = value.indexOf("@");
temp = value.substring(tmp[2]+1, tmp[2]+2);
uint8_t length = temp.toInt();
Serial.print("Command code:"); Serial.print(command_code);
Serial.print("\tlength:"); Serial.println(length);
temp = value.substring(tmp[1]+1, tmp[2]);
Serial.print("\ttemp:"); Serial.println(temp);
for(byte coodi = 0; coodi < length; coodi++){
tmp[3] = temp.indexOf(",");
String cood = temp.substring(0, tmp[3]);
Serial.print("\tcood:"); Serial.print(cood);
tmp[4] = cood.indexOf("(");
tmp[5] = cood.indexOf(".");
tmp[6] = cood.indexOf(" ");
tmp[7] = cood.indexOf(".", tmp[6]);
tmp[8] = cood.indexOf(")");
Serial.print("\tlat:"); Serial.print(cood.substring(tmp[4]+1, tmp[5]));
Serial.print("."); Serial.print(cood.substring(tmp[5]+1, tmp[6]).toInt());
// Serial.print("\tlat:"); Serial.print(cood.substring(tmp[4]+1, tmp[6]).toFloat(), 16);
Serial.print("\tlon:"); Serial.print(cood.substring(tmp[6]+1, tmp[7]));
Serial.print("."); Serial.print(cood.substring(tmp[7]+1, tmp[8]).toInt());
// Serial.print("\tlon:"); Serial.print(cood.substring(tmp[6]+1, tmp[8]).toFloat(), 16); Serial.println();
temp = temp.substring(tmp[3]+1);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
The output is:
$20$(-3.7001624954631467 139.85888350863482),(48.1492194613472 18.3989561222478),(-46.42921396602168 -45.14057884500802),(60.61327990751049 -117.32494850168642)@4
Command code:20 length:4
temp:(-3.7001624954631467 139.85888350863482),(48.1492194613472 18.3989561222478),(-46.42921396602168 -45.14057884500802),(60.61327990751049 -117.32494850168642)
cood:(-3.7001624954631467 139.85888350863482) lat:-3.-666536661 lon:139.1889845370
cood:(48.1492194613472 18.3989561222478) lat:48.1840961760 lon:18.-463395506
cood:(-46.42921396602168 -45.14057884500802) lat:-46.1788413240 lon:-45.456540994
cood:(60.61327990751049 -117.32494850168642) lat:60.152731465 lon:-117.-872392894
You can see the decimal places are wrong with toFloat() or with toInt().
Please suggest a solution or a different code.