[SOLVED] Error while doing int to string

Hello Guys,

I am sorry if this is a dumb question but I cant explain this behavior.

When I am trying to convert from int to String its stops at 100 then as you reach 120 it returns 122, 130->133.

This is the code.

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  for(int a=95;a<999;a++)
  {
    Serial.print("Counter:");
    Serial.println(a);
    Serial.print("String:");
    Serial.println(String(a));
    delay(1000);
  }
}

And this is the output.

Counter:95
String:95
Counter:96
String:96
Counter:97
String:97
Counter:98
String:98
Counter:99
String:99
Counter:100
String:100
Counter:101
String:100
Counter:102
String:100
Counter:103
String:100

If I do the following change it works.

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  for(int a=95;a<999;a++)
  {
    Serial.print("Counter:");
    Serial.println(a);
    Serial.print("String:");
    Serial.println(String(a+0.0));
    delay(1000);
  }
}

Result

Counter:95
String:95.00
Counter:96
String:96.00
Counter:97
String:97.00
Counter:98
String:98.00
Counter:99
String:99.00
Counter:100
String:100.00
Counter:101
String:101.00
Counter:102
String:102.00
Counter:103
String:103.00
Counter:104
String:104.00

I get that It must be a expected behavior but I have been unable to understand why.

Your original code works OK for me
Windows 10
IDE 1.8.10

The real question is, why would you ever convert from an int to a String ?

Thank you for the answer, I have the same
Win 10
IDE 1.8.11

This is part of a larger program that uses an code input like "123A" on a switch case to execute several different functions.

I was running a debug run going thru all possible codes when I noticed that 101~109 was getting turned into 100.

I have another board at home I will try the same code on another board.

UKHeliBob:
Your original code works OK for me
Windows 10
IDE 1.8.10

The real question is, why would you ever convert from an int to a String ?

What board are you using?

Pieter

The original problem was on a standalone atmega328p-pu, that was programed using a Arduino UNO.

I tested with Arduino Duemilanove, and the problem didn't occurred.
I tested with the Arduino UNO original chip and the problem didn't occurred.
I placed the atmega328p-pu again on the Arduino UNO board and problem was fixed.

Problem is solved. I don't know what happened.

PieterP:
What board are you using?

Pieter

This is part of a larger program that uses an code input like "123A" on a switch case to execute several different functions.

It is still not clear why that involves converting an int to a String