473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

how to compare two byte arrays with length not divisible by 4...

This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and added two items discussed in the previous post:
1. different length arrays
2. buffer must be divisible by 4.

I simply check the remaining bytes using the two byte pointers.

Expand|Select|Wrap|Line Numbers
  1. public unsafe static bool CompareByteArrays(byte[] b1, byte[] b2)
  2. {
  3.     if (b1.Length != b2.Length)
  4.         return false;
  5.     fixed (byte* bp1 = b1)
  6.     {
  7.         fixed (byte* bp2 = b2)
  8.         {
  9.             int* ip1 = (int*) bp1;
  10.             int* ip2 = (int*) bp2;
  11.             for (int i = 0; i < b1.Length/4; i++)
  12.             {
  13.                 if (ip2[i] != ip1[i])
  14.                     return false;
  15.             }
  16.             if ((b1.Length % 4) != 0)
  17.             {
  18.             for (int i = b1.Length - (b1.Length % 4); i < b1.Length; i++)
  19.             {
  20.                 if (bp2[i] != bp1[i])
  21.                     return false;
  22.             }
  23.             }
  24.  
  25.             return true;
  26.         }
  27.     }
  28. }

Peace,

Robb
Apr 27 '10 #1
3 4141
tlhintoq
3,525 Expert 2GB
TIP: When you first created your question you were asked to wrap your code with [code] tags.

It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 27 '10 #2
tlhintoq
3,525 Expert 2GB
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed.
If you could include a link to the post you are talking about that might be very helpful
Apr 27 '10 #3
Here is the original post.

Thanks for the correction - the code looks much better. I actually did not go through a question and posted exactly like what I saw in the original (and didn't look for a code button).

I also misspoke when I mentioned the person who answered. It was Marcus Andrén.

That's what I get trying to be quick...

Thanks again!
Apr 27 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
3
by: NathanV | last post by:
I'm trying to compare two byte's. I think I have to use binary operators. How can I tell if two bytes have the same value? Also, I have a function that is returning a byte value. Is there a...
2
by: nobs | last post by:
Hi I have two byte arrays (each with a length of 8 bytes) an I'd like to compare them if they are equal. The operator == shouldn't work because its not a basic Data Type so I used the method...
1
by: Jacob Thastrup | last post by:
Hi is there an easy way to compare arrays without going through each entry in the arrays? Say I have two arrays and I want to find entries that are not present in both. Thanks Jacob Thastrup
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
8
by: Turbot | last post by:
Hello, Anyone know how to compare two byte arrays without going through each item in the array. I actually want to compare two bitmap objects to see if they both contain the same picture bit...
5
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
2
by: Ole | last post by:
How do I compare two byte arrays in a if statement? Thanks Ole
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.