【问题标题】:Convert byte to bool in C#在 C# 中将字节转换为布尔值
【发布时间】:2016-01-28 03:28:04
【问题描述】:

我正在尝试基于 arduino 库 (http://blog.electrodragon.com/rc522-write-a-card-demo-code/) 在 c# 上创建一个物联网核心库,但我不明白是什么:

if (!(temp & 0x03))

while ((i!=0) && !(n&0x01) && !(n&waitIRq))

这是必需的布尔值,但这些是字节! 我如何转换它?

如果有人知道已经制作了一个库,请告诉我。 谢谢。

【问题讨论】:

标签: c# arduino rfid raspberry-pi2 windows-10-iot-core


【解决方案1】:

在 C 中,任何非零表达式都隐含地为真,零为假。在 C# 中,您需要进行显式比较:

if ((temp & 0x03) == 0)

while ((i!=0) && (n&0x01)==0 && (n&waitIRq)==0)

或者,您可以使用the .NET's Boolean structure

bool isTempAppropriate = (temp & 0x03) == 0;
if(isTempAppropriate) { ... }

请注意,bool 只是System.Boolean 的语法糖,您可以使用the var keyword 而不是bool

【讨论】:

  • C# 的括号太多了 :)
  • 这个答案不正确。 C 中的 !(n&0x01) 在 C# 中是 (n&0x01) == 0
  • @MatthewWatson 非常正确,已修复!非常感谢!!
猜你喜欢
  • 2012-03-02
  • 2014-10-11
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多