Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Atoi()函数不工作_C++_Arduino - Fatal编程技术网

C++ Atoi()函数不工作

C++ Atoi()函数不工作,c++,arduino,C++,Arduino,Arduinoatoi()函数未按预期工作。原始版本是656649,但当转换为字符串时,它会打印1289。会发生什么?谢谢 void setup() { Serial.begin(9600); } void loop() { String BT1 = "656649" Serial.print(" String BT1: "); Serial.print(BT1); // OUTPUT: 656649 char charBuf[50]; BT1.toCharArray(

Arduino
atoi()
函数未按预期工作。原始版本是656649,但当转换为字符串时,它会打印1289。会发生什么?谢谢

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);
}

Arduino
int
和'
unsigned int
类型是16位值,太小,无法容纳您使用的数字。您需要
long
unsigned long
类型来保存该值;在ATmega(Arduino)体系结构中,这些是32位长的


Arduino的许多编程问题都源于这些不同的尺寸——因为现在大多数个人计算机都是64位的,所以很容易忘记Arduino系列的核心微控制器是哈佛体系结构的8位机器,带有8位寄存器。

也许Arduino是16位系统
atol
应该可以工作,您应该使用
long
(u)int32_t
来表示32位数字;打印出
sizeof(int)
,得到什么?656649等于0x000A0509;0x0509等于1289 intBT1是否具有数据类型?你打算把它变成int吗?流程是否知道这一点?由于atoi不会抛出任何异常,您可能会看到未定义的行为……该过程正试图帮助您解决问题。哦,是的,对不起,上面定义了intBT1:int intBT1;这意味着您将要使用
atol()
atol()
而不是
atoi()
。但是请注意,
ato*()
函数没有错误检查。