简体   繁体   中英

If condition not evaluating as expected

I'm new to Visual Studio and C#, but my problem seems almost elementary, yet I can't figure it out. I have a binary file I'm reading in and I'm trying to process it more or less a byte at a time. The problem is that when I get several bytes in my 'if' condition seems to evaluate to TRUE long after it should be FALSE.

Here's the quick jist of the input binary so the code below makes sense.

bytes[0-3]: preamble
bytes[4]: message type
bytes[5-7]: message length
bytes[8-11]: test ID

And a snippet of the code

      file = openFileDialog1.FileName;
      int byteLoc = 0;
      try
      {
           var bytes = File.ReadAllBytes(file);
           //loop through each byte from the input file
           foreach (var singleByte in bytes)
           {
                //Preamble - 4 bytes
                if (byteLoc < 4)
                {
                    preamble += Convert.ToString(singleByte);
                    preamble += " ";                        
                }
                //Message Type - 1 byte
                else if (byteLoc == 4)
                {
                    msgType += Convert.ToString(singleByte);
                }
                //Message Length - 3 bytes
                else if ((byteLoc > 4) || (byteLoc <= 7))
                {
                    msgLen += Convert.ToString(singleByte);
                    Console.WriteLine("Len:" + byteLoc);  //for debug
                }
                //Test ID - 4 bytes
                else if ((byteLoc >= 8) || (byteLoc <= 11))
                {
                    testID += Convert.ToString(singleByte);
                    Console.WriteLine("ID:" + byteLoc);   //for debug
                }
                byteLoc++;
           }
      }

I have printed out the preamble and the msgType and they appear as expected. However, the problem is that when I get to the 'if' condition for the msgLen (which should be bytes 5-7), it ALWAYS evaluates to TRUE. I see the message "Len: byteLoc" starting at 5 and going all the way to the end of the file. What am I doing wrong here that the when byteLoc gets to 8, its not going to the next 'if' condition?

else if ((byteLoc > 4) || (byteLoc <= 7))
...
else if ((byteLoc >= 8) || (byteLoc <= 11))

These should be && together rather than ||

Your first condition above will always evaluate to true (after having failed the previous conditions) because byteLoc will be greater than 4. You need to constrain it with an AND , not an OR

The condition (true OR false) will always evaluate to true

Your condition is byteLoc > 4 OR byteLoc <= 7 , which will always be true. For example, 11 is a number that is greater than 4, so that causes the first half to return true, and 3 is a number that is less than or equal to 7, so that will cause the second half to return true. Since you are using OR , only one of the conditions needs to be true for the entire condition to return true.

You want to use && instead of || , since you want your condition to be byteLoc > 4 AND byteLoc <= 7 . That will ensure that the overall condition will only be true if both conditions are true.

As mentioned the issue is that you are using || (OR) instead of && (AND) but since you're doing else if you know the previous conditions are false so you can just use upper bound checks only.

if (byteLoc < 4)
{
    preamble += Convert.ToString(singleByte);
    preamble += " ";                        
}
//Message Type - 1 byte
else if (byteLoc == 4)
{
    msgType += Convert.ToString(singleByte);
}
//Message Length - 3 bytes
else if (byteLoc <= 7)
{
    msgLen += Convert.ToString(singleByte);
    Console.WriteLine("Len:" + byteLoc);  //for debug
}
//Test ID - 4 bytes
else if (byteLoc <= 11)
{
    testID += Convert.ToString(singleByte);
    Console.WriteLine("ID:" + byteLoc);   //for debug
}

I know this is not what you asked for but probably iterating over all the bytes isn't the best solution. Since you have a fixed amount of bytes per type (preamble, message type, message length and testID). You can do something like this:

        FileStream fs = File.OpenRead(@"C:\YourFilePath");
        BinaryReader br = new BinaryReader(fs);
        string preamble = Encoding.Default.GetString(br.ReadBytes(4));
        string msgType = br.ReadByte().ToString();
        string msgLen = Encoding.Default.GetString(br.ReadBytes(3));
        string testID = Encoding.Default.GetString(br.ReadBytes(4));

The short circuit in the if condition is incorrect:

else if ((byteLoc > 4) || (byteLoc <= 7)) will return true for ANY value greater that 4

do instead:

else if ((byteLoc > 4) && (byteLoc <= 7))

etc etc for the other else branches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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