Marco Pizarro-Silva Marco Pizarro-Silva -4 years ago 107
C++ Question

Atoi() Function Not Working Arduino

the Arduino atoi() function is not working as expected. The original is 656649, but when converted to a string, it prints 1289. What could be happening? Thanks!

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
String BT1 = "656649"
Serial.print(" String BT1: ");
Serial.print(BT1); // OUTPUT: 656649
char charBuf[50];
BT1.toCharArray(charBuf, 50) ;
Serial.print("Char buff: "); // OUTPUT: 656649
Serial.print(charBuf);
intBT1 = atoi(charBuf);
Serial.print(" intBT1: "); //OUTPUT: 1289
Serial.print(intBT1);
}

Answer Source

The Arduino int type is a 16-bit value, far too small to hold the number you used. You need a long or unsigned long type to hold that value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download