Atoi(), Not working as expected

can somone explain why atoi() seems not to work here:

I’m perplexed!

using Dev here

EDIT: Ignore this, not actually true.

atoi requires that the whole string be a valid number, or it will return 0. Since command is “3600Button~3~” and therefore not actually a number, it returns 0. In other words, it won’t just take the 3600 and stop parsing.

The gods seem to disagree.

http://www.cplusplus.com/reference/cstdlib/atoi/

http://en.cppreference.com/w/cpp/string/byte/atoi

Huh. Well, that’s what I get for not looking it up before responding. Then I don’t know why it didn’t work!

void setup() {
    Serial.begin(9600);
    
    Serial.printlnf("%d", atoi("12345"));
    Serial.printlnf("%d", atoi("12345xx"));
}
$ particle serial monitor
Opening serial monitor for com port: "/dev/cu.usbmodemFD1161"
12345
12345

Is it possible that value (and therefore command) has non-printable characters in them. Why don’t your print the strlen to see if there are characters you can’t see.

@rickkas7, can you run your example but with the same string shown in @BulldogLowell’s screenshot?

Good thinking.

void setup() {
    Serial.begin(9600);
    
    Serial.printlnf("%d", atoi("3600Button~3~"));
}
3600

Argh!! Either there is something odd embedded in the string we can’t see or I don’t know what’s going on.

@BulldogLowell, can you post your code in a copiable form and also provide the datatypes used for the respective variables?

1 Like

As it turns out… there is a memset function in the Library I was using that took the entire fixed width and filled it with nulls.

But, somehow, and I cannot figure out where ( I actually had to rebuild a large bunch of code) that function was eliminated making the arrays not null-terminated, which atoi() seems to disagree with!

thanks all

2 Likes

I’m surprised the strcpy() didn’t case an SOS and your print statements didn’t show any funny characters.