Reputation: 23
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() {
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);
}
Upvotes: 2
Views: 7223
Reputation: 7409
The Arduino int
and 'unsigned int
types are 16-bit values, too small to hold the number you used. You need a long
or unsigned long
type to hold that value; these are 32 bits long in the ATmega (Arduino) architecture.
Many programming problems with the Arduino stem from these different sizes -- as most personal computer are 64-bit these days, it's easy to forget that the microcontrollers at the heart of the Arduino family are Harvard architecture 8-bit machines with 8-bit registers.
Upvotes: 2