# Bug report for String(Not a bug)

I did encounter a strange behavior when i did use this code:

``````  while(i<11)
{
myGLCD.fillRect((21*i)+3, 250, (21*i)+23, 290);
myGLCD.print((String)(21*i)+3, LEFT, i*20);
}
``````

The LCD did fill everything correct but when i did look at the printed value it did not look right. I got this values:
213;423;633;843;1053;1263;1473;1683;1893;2103;

Then i did but the formula in to a INT and then convert it to String I got the right values:

``````int x=(21*i)+3;
myGLCD.print((String)x, LEFT, i*20);
``````

Witch did give me the following values:
24;45;66;87:108;129;150;171;192;213

It seems (String) do some strange calculation on conversion from int to String.

Pleas let me know if this is a well known bug or if I did something wrong.

ehel:
I did encounter a strange behavior when i did use this code:

``````  while(i<11)
``````

{
myGLCD.fillRect((21i)+3, 250, (21i)+23, 290);
myGLCD.print((String)(21i)+3, LEFT, i20);
}

``````

The LCD did fill everything correct but when i did look at the printed value it did not look right. I got this values:
213;423;633;843;1053;1263;1473;1683;1893;2103;

Then i did but the formula in to a INT and then convert it to String I got the right values:

``````

int x=(21i)+3;
myGLCD.print((String)x, LEFT, i
20);

``````

Witch did give me the following values:
24;45;66;87:108;129;150;171;192;213

It seems (String) do some strange calculation on conversion from int to String.

Pleas let me know if this is a well known bug or if I did something wrong.
``````

Order of operations. 21 * i is calculated, then cast to a String. The plus operator is then applied, which for Strings, is concatenation. Notice how all of the values have a 3 at the end?

Of curse it is. My bad. I'm too tired. I need of course a new par of brackets to make it into a calculation witch then will be translated into a String. Now he just do calculation on the 21*i and then add 3 as a String. Don't know how i did miss that.

Thx.

``````(String)(21*i)+3
``````

i = 4 (for example). Expand: (String)(21*4)+3 -> multiply (String)(84)+3 -> typecast "84"+3 -> concatenate "843"

It's not a bug in String, it's a bug in your code.

I suggest you look up sprintf.

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.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes