473,320 Members | 1,896 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,320 software developers and data experts.

Using binary operators to compare byte[]s

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 way to
see if the return value isnt null.

Thanks!

Nov 16 '05 #1
3 17214
NathanV wrote:
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?
just use ==


Also, I have a function that is returning a byte[] value. Is there a way to see if the return value isnt null.


== null
Usually you should let it return an empty byte[] instead of null.

Nov 16 '05 #2
If you have broken down to byte arrays, it is quite easy to compare two
bytes. If you are trying to compare two bytes out of a Unicode char, or
similar, you will have to break down.

As for the byte array, check the length. If 0, you have nothing. You can
test null first, if you are ever going to return a null object, but it is
more common you will initialize before return (not always true, but more
common). Of course, this will break if you have set the array to a specific
length. You can also pull byte[0] and see if it is 0. This is fine, as long
as none of your bytes are 0. For most byte functions, 0 is a null char, so
it is not valid, but this is not always true.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
"NathanV" <Na*****@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
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 way
to
see if the return value isnt null.

Thanks!

Nov 16 '05 #3
public static bool ArraysEqual(byte[] ba1, byte[] ba2)
{
if ( ba1 == null )
throw new ArgumentNullException("ba1");
if ( ba2 == null )
throw new ArgumentNullException("ba2");

if ( ba1.Length != ba2.Length )
return false;

for(int i=0; i < ba1.Length; i++)
{
if ( ba1[i] != ba2[i] )
return false;
}
return true;
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"NathanV" <Na*****@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
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 way to see if the return value isnt null.

Thanks!


Nov 16 '05 #4

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

Similar topics

7
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran proggie. The program produces a binary file, by...
7
by: vrigo | last post by:
hi folks, is there a fast way to check for large regions of binary zeroes in memory? the solution could be either in C or in assemble language? i am interested in any RISC platforms. TIA, ...
2
by: STEVE.KING | last post by:
Greetings: I need to download an executable and some other binary files (a C++ DLL) from a virtual directory in my WinForms application. No-Touch Deployment manages my .NET code fine but...
3
by: Benny Ng | last post by:
Dear all, The following is the source. The password is encrypted and saved into the Binary in SQL2K. Now I want to create a new page to compare the existed password and the password that in the...
12
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would...
10
by: Craig | last post by:
Hi there, I'm trying to switch binary numbers around so that the MSB becomes the LSB etc. Is there an easy way of doing this as I can't seem to find anything. If you could help that would be...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.