# toInt() bug, how unfortunate

**URL:** <https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669>\
**Category:** Programming\
**Created:** [October 13, 2022, 10:07pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669 "2022-10-13T22:07:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 13, 2022, 10:07pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/1 "2022-10-13T22:07:51Z")

</div>

So the documentation for toInt() says this:

"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?

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [October 13, 2022, 10:18pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/2 "2022-10-13T22:18:57Z")

</div>

Post your test code, please.  
Also, which board did you test this on?

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [October 13, 2022, 10:22pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/3 "2022-10-13T22:22:48Z")

</div>

Hi,  
I tested like this:

```cpp
String a = "FFFFFFFF";

void setup() {
  Serial.begin(115200);
  delay(100);
}

void loop() {
  Serial.print("a results ");
  Serial.println(a.toInt(), HEX);
  while (1) {}
}

```

ad the results:

```cpp
a results 0

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 13, 2022, 10:34pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/5 "2022-10-13T22:34:02Z")

</div>

Post your code because "FFFFFFFF" does return 0 because toInt() does not parse Hex..

> [@axemaster](#):
>
> Is there a good function I can use instead, which will give the correct result?

don't use the String class and use [strtol()](https://cplusplus.com/reference/cstdlib/strtol/) or if you require long long [strtoll()](https://cplusplus.com/reference/cstdlib/strtoll/)

You can use the endptr to assess if the conversion went fine or failed and your hex number can start with 0x (you get to choose the base)

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 13, 2022, 11:21pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/6 "2022-10-13T23:21:17Z")

</div>

> [@axemaster](#):
>
> "Converts a valid String to an integer."

> However, if I send 0xFFFFFFFF, toInt() will convert it to 2147483647

What are you talking about? Modify @ruilviana's sketch and prove this assertion.

You can toss it right into the wokwi simulator, which is where I learned more than I will ever need to about _toInt_().

Or share your own code where something happens for you that doesn't for us.

a7

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [October 13, 2022, 11:37pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/7 "2022-10-13T23:37:39Z")

</div>

toInt returns a 'long'. The reference: [toInt() - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/)

 ![afbeelding](https://europe1.discourse-cdn.com/arduino/original/4X/2/f/c/2fc77f506593afd088b76b8ec75f9bda8ce51728.png)  
It is not good to deal with 32-bits of a unsigned long.  
The source code of toInt is here: [ArduinoCore-API/String.cpp at master · arduino/ArduinoCore-API · GitHub](https://github.com/arduino/ArduinoCore-API/blob/master/api/String.cpp#L739)  
It is just a "`atol(buffer)`" of the buffer which is allocated in the heap.  
I made a test sketch:

```cpp
// 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](https://wokwi.com/projects/345448027421409874)  
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.

---

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 14, 2022, 4:00am UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/8 "2022-10-14T04:00:29Z")

</div>

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.

Thanks for the wonderful responses!

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 14, 2022, 8:52am UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/9 "2022-10-14T08:52:00Z")

</div>

What was your actual hardware??

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 14, 2022, 11:16am UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/10 "2022-10-14T11:16:23Z")

</div>

> [@axemaster](#):
>
> 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.

a7

---

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 14, 2022, 2:48pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/11 "2022-10-14T14:48:47Z")

</div>

My hardware is Teensy 4.1.

I wrote a simple test sketch and ran it on Wokwi and the actual Teensy, and got different results:

```cpp
#include <stdint.h>

void setup() {
  Serial.begin(115200);
}

void loop() {
  String teststr = "4294967295";
  Serial.println(teststr);
  uint32_t temp = (uint32_t)(teststr.toInt());
  Serial.println(String(temp));
  delay(1000);
}

```

On Wokwi, this correctly prints:  
4294967295  
4294967295

On the Teensy hardware, this prints:  
4294967295  
2147483647

---

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 14, 2022, 3:03pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/12 "2022-10-14T15:03:30Z")

</div>

Alright, I found a conversion method that seems to work, using strtoul:

```cpp
#include <stdint.h>
#include <stdlib.h>

void setup() {
  Serial.begin(115200);
}

void loop() {
  String teststr = "4294967295";
  Serial.print("\nOriginal value = ");
  Serial.println(teststr);
  
  uint32_t temp = (uint32_t)(teststr.toInt());
  Serial.print("Using toInt = ");
  Serial.println(String(temp));
  
  uint32_t temp2 = strtoul(teststr.c_str(), NULL, 10);
  Serial.print("Using strtoul = ");
  Serial.println(String(temp2));
  
  delay(2000);
}

```

I found this works on both Wokwi and the Teensy.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 14, 2022, 3:14pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/13 "2022-10-14T15:14:05Z")

</div>

well that's a pain. My advice stands : don't use the String class and use [strtol()](https://cplusplus.com/reference/cstdlib/strtol/) or if you require long long [strtoll()](https://cplusplus.com/reference/cstdlib/strtoll/)

if you want unsigned there is also [strtoul()](https://cplusplus.com/reference/cstdlib/strtoul/)

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 14, 2022, 3:22pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/14 "2022-10-14T15:22:47Z")

</div>

> [@axemaster](#):
>
> I found this works on both Wokwi and the Teensy.

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. 😑

a7

---

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 14, 2022, 3:25pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/15 "2022-10-14T15:25:41Z")

</div>

Fair question. The board in Wokwi was Uno.

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.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 14, 2022, 3:26pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/16 "2022-10-14T15:26:39Z")

</div>

> [@axemaster](#):
>
> In embedded programming writing things like "int" is just begging for trouble after all

So is using the String class on an Uno and the like.

---

<div class="post-metadata">

**Author:** ![axemaster](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@axemaster](https://forum.arduino.cc/u/axemaster)\
**Post date:** [October 14, 2022, 3:27pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/17 "2022-10-14T15:27:40Z")

</div>

> [@anon94801460](#):
>
> So is using the String class on an Uno and the like.

Why?

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 14, 2022, 3:28pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/18 "2022-10-14T15:28:34Z")

</div>

Ah, I see you are unaware of the problems (no/poor garbage collection): [Don't Use Strings](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/8#msg2640860)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [October 14, 2022, 3:42pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/19 "2022-10-14T15:42:22Z")

</div>

> [@axemaster](#):
>
> Why?

Because it can leave holes bigger than those in a swiss cheese in the memory of the microcontroller if incorrectly used.

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/9/0/2/902bc6345d6279573aff5cf92410883034f178ac.jpeg)  
Source: [File:Emmental 015.jpg - Wikipedia](https://en.wikipedia.org/wiki/File:Emmental_015.jpg)

One explanation: [The Evils of Arduino Strings | Majenko's Hardware Hacking Blog](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/)

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [October 14, 2022, 3:43pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/20 "2022-10-14T15:43:50Z")

</div>

> [@axemaster](#):
>
> `uint32_t temp = (uint32_t)(teststr.toInt());`

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.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 14, 2022, 3:56pm UTC](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669/21 "2022-10-14T15:56:19Z")

</div>

> [@RayLivingston](#):
>
> So, it gets converted to the largest possible value, which is 2147483647

_the proof of the pudding is in the eating_

```arduino
void setup() {
  Serial.begin(115200);

  String v = "4294967295";
  int32_t signedData = v.toInt(); // really to a long
  uint32_t unsignedData = signedData;
  uint32_t unsignedDataC = strtoul(v.c_str(), nullptr, 10);

  Serial.println(signedData);
  Serial.println(unsignedData);
  Serial.println(unsignedDataC);
}

void loop() {}

```

[Next page](https://forum.arduino.cc/t/toint-bug-how-unfortunate/1041669.md?page=2)
