PC Review


Reply
Thread Tools Rate Thread

Best way to compare two BYTE arrays?

 
 
Tom
Guest
Posts: n/a
 
      12th Aug 2004
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 loaded
Dim data64_1 As String = System.Convert.ToBase64String(result1, 0,
result1.Length)
Dim data64_2 As String = System.Convert.ToBase64String(result2, 0,
result2.Length)
Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))

Seems like there should be an easier way.... but neither IS nor = works, and
there isn't any Compare verb or anything. I could, obviously, loop thru each
one and compare each byte, but that seems like too much effort.

What am I missing?

Tom


 
Reply With Quote
 
 
 
 
Wardeaux
Guest
Posts: n/a
 
      12th Aug 2004
Tom,
Seems like it's uglier, but it is faster and most ways simpler just to
run a tight "For" loop to compare array element by element, especially if
the array is good sized. If you use it in a lot of places, just write an
""IsEqual(mb1, mb2)" function that returns boolean... something like this...
function IsEqual(ByRef mb1 as byte(), ByRef mb2 as Byte()) as boolean
Dim bRetVal as boolean
Dim index as long

if (mb1.length <> mb2.length) then ' make sure arrays same length
bRetVal = false ' if not quit here... not same
else
for (index = 0 to mb1.length-1) ' run array length looking for
miscompare
if (mb1(index) <> mb2(index)) then
bRetVal = false
exit for
end if
next
end if
return bRetVal
end funtion

then just call the function whereever you need... much leaner than
converting to base64 twice and then compare... especially for larger
arrays...

Hope this helps!!
wardeaux

"Tom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 loaded
> Dim data64_1 As String = System.Convert.ToBase64String(result1, 0,
> result1.Length)
> Dim data64_2 As String = System.Convert.ToBase64String(result2, 0,
> result2.Length)
> Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
>
> Seems like there should be an easier way.... but neither IS nor = works,

and
> there isn't any Compare verb or anything. I could, obviously, loop thru

each
> one and compare each byte, but that seems like too much effort.
>
> What am I missing?
>
> Tom
>
>



 
Reply With Quote
 
Wardeaux
Guest
Posts: n/a
 
      12th Aug 2004
Oh yea, don't forget to initialize bRetVal to True...
wardeaux

"Wardeaux" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Tom,
> Seems like it's uglier, but it is faster and most ways simpler just to
> run a tight "For" loop to compare array element by element, especially if
> the array is good sized. If you use it in a lot of places, just write an
> ""IsEqual(mb1, mb2)" function that returns boolean... something like

this...
> function IsEqual(ByRef mb1 as byte(), ByRef mb2 as Byte()) as boolean
> Dim bRetVal as boolean
> Dim index as long
>
> if (mb1.length <> mb2.length) then ' make sure arrays same length
> bRetVal = false ' if not quit here... not same
> else
> for (index = 0 to mb1.length-1) ' run array length looking for
> miscompare
> if (mb1(index) <> mb2(index)) then
> bRetVal = false
> exit for
> end if
> next
> end if
> return bRetVal
> end funtion
>
> then just call the function whereever you need... much leaner than
> converting to base64 twice and then compare... especially for larger
> arrays...
>
> Hope this helps!!
> wardeaux
>
> "Tom" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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 loaded
> > Dim data64_1 As String = System.Convert.ToBase64String(result1, 0,
> > result1.Length)
> > Dim data64_2 As String = System.Convert.ToBase64String(result2, 0,
> > result2.Length)
> > Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
> >
> > Seems like there should be an easier way.... but neither IS nor = works,

> and
> > there isn't any Compare verb or anything. I could, obviously, loop thru

> each
> > one and compare each byte, but that seems like too much effort.
> >
> > What am I missing?
> >
> > Tom
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Performance File Compare: MD5/SHA1 or Byte-by-Byte Checking? Mahmoud Al-Qudsi Microsoft C# .NET 6 4th Apr 2007 04:48 PM
How to compare two byte arrays? Ole Microsoft C# .NET 2 23rd Jun 2006 01:09 PM
How to compare two byte arrays ? Oleg Subachev Microsoft C# .NET 5 19th Apr 2006 02:13 PM
Re: HELP! compare two SQL timestamps in C# byte arrays - My Solution Next Microsoft C# .NET 5 20th Sep 2004 06:48 PM
HELP! compare two SQL timestamps in C# byte arrays Next Microsoft ADO .NET 9 20th Sep 2004 06:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:26 AM.