Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having:
C#
byte[] buffer;
if((buffer[6])&(0x01))
{

}

I want to check the last bit in the 6th byte should be one so I am applying this but it throws me error: Cannot implicitly convert type 'int' to 'bool'.
Posted
Updated 13-Sep-12 6:28am
v2

Cannot implicitly convert type 'int' to 'bool'.
Looks like your expression in the IF statement is not evaluating as a boolean value. Probably a wrong '&' operator?
C#
byte[] buffer;
byte one = 1 ;
if((buffer[6])== one)


Or:
Use MSDN: Byte.CompareTo Method (Byte)
[^]
C#
if(one.CompareTo(buffer[6]))
{
}
 
Share this answer
 
v2
Comments
Kuthuparakkal 13-Sep-12 21:16pm    
my 5+
Matt T Heffron 19-Oct-12 21:16pm    
This doesn't do what OP stated as the problem.
Checking *the low-order bit* of buffer[6] is 1,
not: checking if buffer[6] == 1
(aside: buffer[6] is actually the 7th byte)
hello,

Try this:

C#
byte[] teste = new byte[3];
teste[0] = 0x01;
teste[1] = 0x02;
teste[2] = 0x03;

if (teste[2].Equals(0x03))
{
}
else
{
}
 
Share this answer
 
The problem is that your if statement is computing a numeric value with the bit-wise AND operator. c# does not implicitly convert integer zero/non-zero to boolean false/true.
Just compare the result of the AND against zero:
C#
if((buffer[6] & 0x01) != 0)
{
  // low-order bit is set
}

If you needed to check for multiple bits at the same time, compare against the bitmask:
C#
if((buffer[6] & 0x81) == 0x81)
{
  // both highest-order and low-order bits (binary: 10000001) are set
}
 
Share this answer
 
If I am understanding your question/statement correctly, try this:

C#
byte[] buffer;
if (buffer[6].ToString().EndsWith("1"))
{
  //do stuff
}


Make sure your byte array actully has items in it.
 
Share this answer
 
Comments
Matt T Heffron 13-Sep-12 19:26pm    
What makes you think the last character of the decimal representation of the value in buffer[6] is the character '1' when the low bit of the value is set?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900