Search code examples
c#arduinorfidraspberry-pi2windows-10-iot-core

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:

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.


Solution

  • In C any non-zero expression is implicitly true, and zero is false. In C# you need to do an explicit comparison:

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

    and

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

    Alternatively, you can use the .NET's Boolean structure:

    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.