SiroDJ
Lifetime Supporting Member
I ran into a slight problem when doing some work. I found the solution to the problem, but, the two lines of code read the same to me. One works, the other doesn't. I made a condition to compare two byte arrays. At least that's what I thought I did.
I know that buffer should always have a size of three bytes in this instance because I use a method to grab only three bytes.
These are the variable types I'm using for buffer and SlaveID:
I'm often resizing the buffer so I'm only catching what I know I want. In this case I want the buffer to have a size of 3.
These logically appear the same to me. Am I seeing something wrong?
Here's an example that works. This just leave me stuck on "HUH?" It seems you can't use "==" to directly compare byte arrays. If I had to, I know I could make a function that could accomplish this sequentially using a FOR loop for arrays of large sizes.
I know that buffer should always have a size of three bytes in this instance because I use a method to grab only three bytes.
These are the variable types I'm using for buffer and SlaveID:
Code:
public static byte[] buffer;
public static int SlaveID;
I'm often resizing the buffer so I'm only catching what I know I want. In this case I want the buffer to have a size of 3.
Code:
buffer = new byte[3];
These logically appear the same to me. Am I seeing something wrong?
Code:
//This works.
buffer[0] == 0x4E && buffer[1] == (byte)(SlaveID + 0x20) && buffer[2] == 0x06
//This doesn't work.
buffer == new byte[] { 0x4E, (byte)(SlaveID + 0x20), 0x06 }
Here's an example that works. This just leave me stuck on "HUH?" It seems you can't use "==" to directly compare byte arrays. If I had to, I know I could make a function that could accomplish this sequentially using a FOR loop for arrays of large sizes.
Code:
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
byte[] buffer = new byte[] { 0x4E, 0x21, 0x06 };
int SlaveID = 0x01;
Console.WriteLine(buffer[0] == 0x4E && buffer[1] == (byte)(SlaveID + 0x20) && buffer[2] == 0x06);
Console.WriteLine(buffer == new byte[] { 0x4E, (byte)(SlaveID + 0x20), 0x06 });
Console.WriteLine(new byte[] { 0x21 } == new byte[] { (byte)(SlaveID + 20) });
Console.ReadKey(true);
}
}
}
/*
true
false
false
*/