473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there a way to compare array values without going through each element?

I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?

Example:
byte[] lTest1 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest2 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest3 = new byte[3] {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different",
"Debug");
if(lTest1 == lTest2)
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different",
"Debug");if(lTest1 == lTest3)
if(lTest1 == lTest3)
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the different",
"Debug");
These all print out TestX and TestY are different. Are there any classes or
methods for comparing arrays by their value?

Thanks,
jim
Nov 16 '05 #1
5 2973
Can't you put the elements behind each other in a string and then just
compare the strings?

"Jim H" <no****@jimsaccount.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?

Example:
byte[] lTest1 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest2 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest3 = new byte[3] {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");
if(lTest1 == lTest2)
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");if(lTest1 == lTest3)
if(lTest1 == lTest3)
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the different", "Debug");
These all print out TestX and TestY are different. Are there any classes or methods for comparing arrays by their value?

Thanks,
jim

Nov 16 '05 #2
Adrian <aa@aa.aa> wrote:
Can't you put the elements behind each other in a string and then just
compare the strings?


If you're going to do that, you might as well save the extra memory etc
and just compare the byte arrays yourself.

There's nothing in the framework to compare the contents of arrays,
although it's easy to write yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
This may be a good occasion to develop your own type to hold the byte data,
rather than using the standard Array provided by C#. The Array class itself
can't be subclassed by applications, and things like ArrayLists may be too
heavyweight, but I'd bet that you could easily create a small and simple
class (or structure, if that suited how these things' lifetimes are managed)
that exposes a Compare method that does just what you're after. Perhaps the
class might contain an array to hold the actual data, and could then provide
an indexer to make it behave much like a simple array.

Regards,
Tom Dacon
Dacon Software Consulting

"Jim H" <no****@jimsaccount.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?

Example:
byte[] lTest1 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest2 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest3 = new byte[3] {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");
if(lTest1 == lTest2)
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");if(lTest1 == lTest3)
if(lTest1 == lTest3)
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the different", "Debug");
These all print out TestX and TestY are different. Are there any classes or methods for comparing arrays by their value?

Thanks,
jim

Nov 16 '05 #4
Tom Dacon <td****@community.nospam> wrote:
This may be a good occasion to develop your own type to hold the byte data,
rather than using the standard Array provided by C#. The Array class itself
can't be subclassed by applications, and things like ArrayLists may be too
heavyweight, but I'd bet that you could easily create a small and simple
class (or structure, if that suited how these things' lifetimes are managed)
that exposes a Compare method that does just what you're after. Perhaps the
class might contain an array to hold the actual data, and could then provide
an indexer to make it behave much like a simple array.


For most purposes, I suspect it would be simpler to implement IComparer
in a way which can compare the byte arrays. It depends on exactly what
the requirements are, of course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Thanks everyone. I guess I'll have to do it myself. I know it's not hard
but I thought there might have been something equivelant to memcmp() in
C/C++. No big deal. I'm just getting used to the .NET framework and it
will take a while for me to get used to what's provided and what's not.

Thanks again,
jim

"Jim H" <no****@jimsaccount.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?

Example:
byte[] lTest1 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest2 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest3 = new byte[3] {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");
if(lTest1 == lTest2)
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different", "Debug");if(lTest1 == lTest3)
if(lTest1 == lTest3)
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the different", "Debug");
These all print out TestX and TestY are different. Are there any classes or methods for comparing arrays by their value?

Thanks,
jim

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
8
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
11
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
3
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
What is the best method to compare 2 array to knw if each element is equal? Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in structure. I tried to write this routine:...
11
by: dusk | last post by:
Hi, I'm very new to javascript, and I'm having trouble finding a way to display a string of divs without the code getting really messy. I have several different pages to write, each with about...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.