我使用 Eclipse 与 Arduino 一起工作。我想从配置文件中读取双精度。我做了这些功能:
void Settings::readDouble(char buff, String data, bool flag, double dataToStore) {
data = "";
int intPart = 0;
int probablyFloatPart = 0;
while(buff != '\n') {
if(isdigit(buff)) {
data.concat(buff);
Serial.println(data);
}
else if (buff == '.') {
char charBuf[data.length()+1];
data.toCharArray(charBuf,data.length()+1);
// Convert chars to int
intPart = atoi(charBuf);
Serial.println(intPart);
data="";
flag = false;
}
buff = theSettings.read();
};
// Convert string to array of chars
char charBuf[data.length()+1];
data.toCharArray(charBuf,data.length()+1);
// Convert chars to integer
probablyFloatPart = atoi(charBuf);
Serial.println(probablyFloatPart);
if (!flag) {
dataToStore = intPart + (probablyFloatPart/pow(10, data.length()));
}
else {
dataToStore = probablyFloatPart;
}
flag = true;
//Serial.println(dataToStore, 5);
}
从 serial.printing 我注意到,如果我要读取的数字在整数或小数部分上超过四位,则会出错。基本上,我不能将超过四个字节的字符串转换为 int。我怎样才能做到这一点?