1

I'm coding a program to parse xml to a byte array, and need to check the numeric values at certain indexes within the byte array.

For example if 99 is an integer within the array, it should write an error message to the console, same for 999 and 01.

What I've tried so far is the using a for each loop, but the if..else conditions are checking the index number, not the integer numbers associated with each index in the array.

Does anyone know how I can loop through the integer values associated with each index? (shown in the screen shot below)

This is what I've tried so far, but the loop seems to be checking the index values , not the integer values associated with the indexes, giving false results:

private static byte[] requestBytes;

foreach(char i in requestBytes)
{
    if (i == 99)
    {
        Console.WriteLine("Sending Failure..");
    }
    else if(i == 999)
    {
        Console.WriteLine("Message Failure..");
    }
    else if (i == 01)
    {
        Console.WriteLine("Sending Success..");
    }

}

As you can see in the picture, each index has an associated integer value, which I want to check in a loop.

Byte array contents

3
  • Why is your loop variable of type char instead of byte? Commented Oct 28, 2015 at 0:05
  • arr[i] with a regular for loop Commented Oct 28, 2015 at 0:09
  • 3
    How can 999 possibly be a value in an element of the byte array? Commented Oct 28, 2015 at 0:12

1 Answer 1

2

You could try a for loop

for(int i = 0; i < requestBytes.Length; i++)
                {
                    var v = requestBytes[i];
                    if (v == 99)
                    {
                        Console.WriteLine("Sending Failure..");
                    }
                    else if(v == 999)
                    {
                        Console.WriteLine("Message Failure..");
                    }
                    else if (v == 01)
                    {
                        Console.WriteLine("Sending Success..");
                    }

                }
Sign up to request clarification or add additional context in comments.

2 Comments

okay will try that solution instead, there is a small typo above..should be for, not foreach.
Yes, sorry my mistake, I was sleepy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.