473,775 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17237
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*****@discus sions.microsoft .com> wrote in message
news:B7******** *************** ***********@mic rosoft.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(byt e[] ba1, byte[] ba2)
{
if ( ba1 == null )
throw new ArgumentNullExc eption("ba1");
if ( ba2 == null )
throw new ArgumentNullExc eption("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*****@discus sions.microsoft .com> wrote in message
news:B7******** *************** ***********@mic rosoft.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
1876
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 itself no problem... each record needs to be converted into std::vector<mystruct> I'm having a helluva time with the binary-->(pod)datatype conversion. Although I've referred to C++ containers, iostreams
7
2653
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, vrigo
2
14213
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 auxilliary files such as these must be downloaded "manually" in my code. My text-based files download fine but I've ran into a problem downloading an .EXE. When it downloads the file size is the same but it has been altered somehow because a) the...
3
3767
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 database. But I don't know how to used source code to solve it. Can you help me? Urgently! <<<<<<Save Method<<<<<<
12
16169
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 result in premature end of input. Instead, you can use the library function feof(), which can be used for both binary- and text-mode files: int feof(FILE *fp);
10
9351
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 great. Thanks and good luck. Craig
15
3004
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 successfully created and used txt files. I am trying to save then load in an array of pointers to objects:
3
14571
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 same length as Replace string. Here's what I have so far. I spent many hours googling for sample code but couldn't find much. Thanks... public static void FindReplace(string OldFile, string NewFile) { string sFind = "ABC"; //I probably need...
53
5257
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 language —I mean, it is not like C, in that sense. I am working on my PhD Thesis, which is about Operations Research,
0
9625
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10280
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.