arduino int转char_将char *转换为int arduino(Convert char* to int arduino)

将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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值