将char *转换为int arduino(Convert char* to int arduino)
我有问题从char获取int值..这里
Serial.println(((char *)“3772837903”)); Serial.println(atoi((char *)“3772837903”));
3772837903 2147483647
有人可以帮忙吗? 谢谢
I have problem to get int value from char.. here
Serial.println(((char*) "3772837903")); Serial.println(atoi((char*) "3772837903"));
3772837903 2147483647
anybody can help? thx
原文:https://stackoverflow.com/questions/40060746
2020-09-28 06:09
满意答案
int是基于AVR的MCU上的16位宽签名变量。 你的显然是一个32位的签名变量,所以我认为它是一个基于ARM的板。
无论如何4294967296-1是无符号整数( uint32_t )的uint32_t 。 对于int32_t它只有一半,即2147483647 。
回报价值
成功时,该函数将转换后的整数作为int值返回。
如果转换后的值超出int的可表示值范围,则会导致未定义的行为。 如果可能的话,请参阅strtol以获得更强大的跨平台替代方案。
我的猜测是,在实践中,这种“未定义的行为”将返回INT_MAX 。
int is a 16-bit wide signed variable on AVR-based MCUs. Yours is obviously a 32-bit signed variable so I presume that it's an ARM-based board.
Anyway 4294967296-1 is the maximum for an unsigned integer (uint32_t). For int32_t it's half of that, i.e. 2147483647.
And about atoi:
Return Value
On success, the function returns the converted integral number as an int value.
If the converted value would be out of the range of representable values by an int, it causes undefined behavior. See strtol for a more robust cross-platform alternative when this is a possibility.
My guess is that, in practice, this "undefined behavior" will be returning INT_MAX.