[英]How to convert a char to int in Arduino
I receive some data in a char variable, and the result in teststring is always a number. 我在char变量中收到一些数据,teststring中的结果总是一个数字。 How can I convert this number to a variable int?
如何将此数字转换为变量int?
After that I can put the int variable on delay time. 之后我可以把int变量放在延迟时间上。 There is a piece of my code:
有一段我的代码:
String readString = String(30);
String teststring = String(100);
int convertedstring;
teststring = readString.substring(14, 18); (Result is 1000)
digitalWrite(start_pin, HIGH);
delay(convertedstring); // Result of teststring convert
digitalWrite(start_pin, LOW);
Use: 使用:
long number = atol(input); // Notice the function change to atoL
Or, if you want to use only positive values: 或者,如果您只想使用正值:
Code: 码:
unsigned long number = strtoul(input, NULL, 10);
Reference: http://www.mkssoftware.com/docs/man3/atol.3.asp 参考: http : //www.mkssoftware.com/docs/man3/atol.3.asp
Or, 要么,
int convertedstring = atoi(teststring.c_str());
Do you have access to the atoi function in your Arduino environment? 您是否可以访问Arduino环境中的atoi功能?
If not, you can just write some simple conversion code in there: 如果没有,你可以在那里写一些简单的转换代码:
int my_atoi(const char *s)
{
int sign=1;
if (*s == '-')
sign = -1;
s++;
int num = 0;
while(*s)
{
num = ((*s)-'0') + num*10;
s++;
}
return num*sign;
}
String to Long Arduino IDE: 字符串到Long Arduino IDE:
//stringToLong.h
long stringToLong(String value) {
long outLong=0;
long inLong=1;
int c = 0;
int idx=value.length()-1;
for(int i=0;i<=idx;i++){
c=(int)value[idx-i];
outLong+=inLong*(c-48);
inLong*=10;
}
return outLong;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.