首页 文章

计算传感器读数中的位数(Arduino)

提问于
浏览
0

我正在创建arduino项目,它检查温度并将数据发送到Xively protal . 我找到了一些例子,但我不明白传感器读取方法中的位数 . 有谁可以向我解释一下?特别是有股息的部分/ 10?

方法是:

//This method calulates the number of digits in the sensor readind
//Since each digit of the ASCII decimal representation is a byte, the number
//of digits equals the numbers of bytes:

int getLength(int someValue)
{
//there's at least one byte:
int digits = 1;
//continually divide the value by ten, adding one to the digit count for
//each time you divide, until you are at 0
int getLength(int someValue) {
int digits = 1; 
int dividend = someValue /10 ;
while (dividend > 0) {
  dividend = dividend /10;
  digits++; 
}
return digits;
}

我真的很感激任何解释

1 回答

  • 1

    当然 . 如果我有1234号码,我想知道有多少位数?好吧,我从1开始,因为我知道至少有1.然后我除以10,这给了我123.那大于0所以我知道至少还有一个数字 . 然后我除以10,这给了我12,大于10,所以我知道至少还有一个数字 . 再次除以10,我得到1.大于0,这又是一个数字 . 再分十,我得到0.现在我知道我已经计算了1234年的所有数字 .

    基本上你使用除以10来删除数字的最后一位数 . 如果这仍然留有一个数字,那么有更多的数字 . 一遍又一遍地做到这一点,直到你达到0.一旦你到0,你就把它们全部嚼掉了,并且已经算完了 .

    这只是数学,而不是编程的任何深奥 .

相关问题