"Converts a valid String to an integer."
"If no valid conversion could be performed... a zero is returned."
Unfortunately, it doesn't actually do that. toInt() is perfectly happy to perform illegal string->int conversions.
For example, I wrote a custom terminal that parses commands in the format "-command val1 val2". val1 and val2 are 32bit binary data, sent as decimal strings. I use toInt() to parse them into binary. However, if I send 0xFFFFFFFF, toInt() will convert it to 2147483647, which is 0x7FFFFFFF. It should have returned 0.
Is there a good function I can use instead, which will give the correct result?
// For: https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669
//
// toInt reference:
// https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
// It returns a 'long', that is a signed long, 32 bits on a Arduino Uno
//
// If the return value of toInt is converted from long to unsigned long,
// it returns the right bits. That is awkward.
void setup()
{
Serial.begin(115200);
unsigned long a = 0xFFFFFFFF;
Serial.print( "a = ");
Serial.print( a); // print as decimal
Serial.print( " = 0x");
Serial.println( a, HEX); // print as hexadecimal
String s = String(a); // use as decimal number
Serial.println( "s = \"" + s + "\"");
long b = s.toInt(); // convert to binary
Serial.print( "b = ");
Serial.println( b);
unsigned long c = (unsigned long) s.toInt(); // use it as unsigned
Serial.print( "c = ");
Serial.println( c);
}
void loop() {}
That is awkward, when the decimal number is converted via a signed long of "atol()", and converted to unsigned long, then it is still the right number
The sketch with a Arduino Uno in Wokwi simulator: toInt.ino - Wokwi Arduino and ESP32 Simulator
It is possible to make changes and test it, but you need to log in to be able to store the sketch to your own Wokwi projects.
I tried running my code on Wokwi and it gives a different result (the correct result) compared to when I ran it on the actual hardware... I will try to figure this out tomorrow.
Please do. Any infidelity of the wokwi will be of great interest to Urish and his boys over there doing the heroic dev work which gives us wokwi.
As you can imagine, if this kind of deficiency exists, it makes the simulator almost useless less useful.
wokwi uses the Arduino tool chain to create the code which then runs on a full-up simulator of the chips that run the code on the various Arduino boards it covers. The several problems I ever knew about were traced to errors or omissions in the chip emulation part.
When you had the flaw with toInt(), what board did you select in the wokwi? Since it does not emulate the Teensy, I hope at least you selected a board with int32_t integers as default.
If you did not, the behaviour is entirely normal/explainable. By someone.
However, in my code I always specify exact types (i.e. uint32_t) to avoid this issue. In embedded programming writing things like "int" is just begging for trouble after all...
I would also point out - it's weird that the bug is on Teensy, which is 32 bit. The Uno on Wokwi had the correct behavior.
Think about what the above is doing. toint() converts the String to an int, NOT a uint. Your input, "4294967295", is 2X the maximum value for a 32-bit int. So, it gets converted to the largest possible value, which is 2147483647. The fact that you then cast the result of uint32_t changes nothing. So, toint() is working exactly as it should, and the wokwi simulation is wrong.
If you want it to convert to a uint32_t, you have to write your own function to parse the String to uint32_t. toint will not do it.