469,282 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 17029
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Drake | last post: by
7 posts views Thread by vrigo | last post: by
12 posts views Thread by Registered User | last post: by
10 posts views Thread by Craig | last post: by
15 posts views Thread by JoeC | last post: by
3 posts views Thread by mouac01 | last post: by
53 posts views Thread by Vicent Giner | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.