C# Byte Arrays - A Comparison Using "=="

SiroDJ

Lifetime Supporting Member
Join Date
Nov 2015
Location
Pennsylvania
Posts
10
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:
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
*/
 
The second one is comparing pointers, the address of "buffer" and the address (on the stack) of the new buffer.

C# is not my first, second, third, fourth or fifth language. ( I wrote one small program in C#)

My guess.
 
Thanks, Mark. It wasn't behaving like I thought it was supposed to, but, that's only due to my shallow understanding of it.
 

Similar Topics

I'm having trouble byte swapping data in RSLogix 5000. I'm reading data from a couple of power analyzers and controllers for refrigeration...
Replies
5
Views
5,025
Our system utilises INPUT_BYTE to capture NMEA0183 ASCII stream which sends data to the NOM multiple times a second. The NOM module can either be...
Replies
0
Views
386
Hello everyone, friends. I need help with something. I want to read and change Bit and Byte numbers via HMI. I found a code snippet for this as...
Replies
18
Views
3,027
Hello everyone :) I just want to start with learning PLC programming, so I need advice. I have SIMATIC S7-1200, CPU with integrated memory...
Replies
5
Views
926
I have a C-More HMI that changes my PLC String from "Machine Status" to "aMhcni etStasu" . There is an option with other objects that have string...
Replies
15
Views
3,469
Back
Top Bottom