简体   繁体   English

在 C# 中将字节转换为布尔值

[英]Convert byte to bool in C#

I'm trying make a library to iot core on c# based on arduino library ( http://blog.electrodragon.com/rc522-write-a-card-demo-code/ ) but i don't understand what is:我正在尝试基于 arduino 库( http://blog.electrodragon.com/rc522-write-a-card-demo-code/ )在 c# 上创建一个库来实现物联网核心,但我不明白什么是:

if (!(temp & 0x03))

or或者

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

It's required boolean values but those are bytes!它是必需的布尔值,但那些是字节! How i can convert this?我怎么能转换这个?

If anyone know a library already made, please let me know.如果有人知道已经制作的图书馆,请告诉我。 Thank you.谢谢你。

In C any non-zero expression is implicitly true, and zero is false.在 C 中,任何非零表达式都隐式为真,零为假。 In C# you need to do an explicit comparison:在 C# 中,您需要进行显式比较:

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

and

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

Alternatively, you can use the .NET's Boolean structure :或者,您可以使用.NET 的布尔结构

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

Note that bool is just syntactic sugar for System.Boolean , and that you could have used the var keyword instead of bool .请注意, bool只是System.Boolean语法糖,您可以使用var关键字而不是bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM