【问题标题】:Using long ints with Arduino Uno在 Arduino Uno 中使用长整数
【发布时间】:2017-12-11 20:50:32
【问题描述】:

我正在尝试获取 100,000 到 10,000 之间的数字,并将每个单独的数字推入队列。

我使用模数函数来做到这一点。但是,我遇到了 Uno 的 16 位整数的问题,其上限为 32,767。我尝试过使用长整数、无符号整数和无符号长整数,但程序仍然只在 32,767 之后将 0 推入队列。我应该尝试 char/string 方法,还是缺少解决方案?


void fill_que(unsigned int b) {
  int price = b;
  while(price > 0) {
    queue.push(price%10);
    Serial.print(price%10);
    price/=10;
  }
}

int main() {
  unsigned int price1 = 36111;
  fill_que(price1);
}

【问题讨论】:

  • 除非我们看到您的尝试,否则无法提供答案。请发帖minimal reproducible example
  • 您是否考虑过使用指针来访问 4 字节的内存存储?这样,如果您使用自己定义的内存区域而不是整数变量,则可能可以解决您的问题……我的意思是,您甚至可以创建自己的“整数类型”
  • @wBB 我该怎么做?
  • 您可以在互联网上的许多视频中看到有关如何使用指针的理论。我现在研究了这两个,我认为它可能会对你有所帮助:1)指针和数组packtpub.com/mapt/book/networking_and_servers/9781787120099/7/… 2)用 c/c++/Arduino 编程youtube.com/…
  • @wBB 为什么使用指针?您只会冒险陷入未定义的行为而没有任何收获。长整数类型应该足够了。

标签: arduino int 16-bit


【解决方案1】:

我猜一下,你忘了改变量price的类型。

应该是:

void fill_que(unsigned long b)
{
  unsigned long price = b;
  while(price > 0)
  {
    queue.push(price%10);
    Serial.print(price%10);
    price/=10;
  }
}

int main()
{
  unsigned long price1 = 36111;
  fill_que(price1);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 2012-07-03
    • 1970-01-01
    • 2013-10-22
    • 2013-06-05
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多