Re: working with byte arrays

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 11/12/04


Date: Fri, 12 Nov 2004 12:22:05 -0500


> I have also made a function
> IsEqualByte(a() as byte,b() as byte) as boolean
> because you can not do something like
> if a=b then...
> My IsEqualByte just compares each byte of both arrays, to my surprise
a byte
> array comparisson with 1MB of size was very fast
> for i=0 toUBound(a) 'as I use the bytearrays as a replacement for
strings
> all arrays start at index 0
> if a(i)<>b(i) then...
> next i
> If you know a better way to compare two byte arrays then please let me
know.

Not sure if this is "better", but you could use your just-gained
knowledge of InstrB...

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  If InStrB(1, A, B) = 1 And _
     LBound(A) = LBound(B) And UBound(A) = UBound(B) Then
    AreArraysEqual = True
  End If
End Function

or, as a one-liner....

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  AreArraysEqual = (InStrB(1, A, B) = 1 And _
                    LBound(A) = LBound(B) And UBound(A) = UBound(B))
End Function

Now, the criteria for equality above is that not only are they
byte-for-byte equivalent, but that the two arrays both have the same
lower and upper bounds. If you always start all of your arrays with the
same lower bound, then these can be simplified to these...

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  If InStrB(1, A, B) = 1 And _
     UBound(A) = UBound(B) Then
    AreArraysEqual = True
  End If
End Function

or, as a one-liner....

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  AreArraysEqual = (InStrB(1, A, B) = 1 And _
                             UBound(A) = UBound(B))
End Function

Now, if you don't care about the equality of the array bounds, only that
the arrays are the same size and that each of their positional
equivalent values are equal, then the above could be modified as
follows...

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  If InStrB(1, A, B) = 1 And _
     UBound(A) - LBound(A) = UBound(B) - LBound(B) Then
    AreArraysEqual = True
  End If
End Function

or, as the one-liner...

Function AreArraysEqual(A() As Byte, B() As Byte) As Boolean
  AreArraysEqual = (InStrB(1, A, B) = 1 And _
                    UBound(A) - LBound(A) = UBound(B) - LBound(B))
End Function

Rick - MVP



Relevant Pages

  • Re: How old is the average Fortran programmer?
    ... be 1 using an Option Base statement. ... The thing that's counterintuitive about the default for VB arrays, if I remember right, is that the lower bound is 0 and you're specifying the upper bound, not the length. ... the author apparently doesn't understand is not a boolean. ...
    (comp.lang.fortran)
  • Re: what am i doing wrong here?
    ... I've written a Java method that uses a list to return 5 three-dimensional arrays containing 'Big Decimal' entries. ... However I also want to return a single boolean type variable, but cant seem to get the program to do this. ... I have declared the boolean type LDMexists, so I cant see what I'm doing wrong here - how do I get it to return the five arrays and the boolean variable LDMexists? ...
    (comp.lang.java.programmer)
  • Re: what am i doing wrong here?
    ... I've written a Java method that uses a list to return 5 three-dimensional arrays containing 'Big Decimal' entries. ... However I also want to return a single boolean type variable, but cant seem to get the program to do this. ... I have declared the boolean type LDMexists, so I cant see what I'm doing wrong here - how do I get it to return the five arrays and the boolean variable LDMexists? ... This adds a reference to the Boolean object to the arraylist. ...
    (comp.lang.java.programmer)
  • Re: working with byte arrays
    ... faster than my current solution because the whole comparisson is done inside ... Thanks for the hint to check the size of the arrays, ... BAs Byte) As Boolean ... > same lower bound, then these can be simplified to these... ...
    (microsoft.public.vb.general.discussion)
  • Re: what am i doing wrong here?
    ... I've written a Java method that uses a list to return 5 three-dimensional arrays containing 'Big Decimal' entries. ... However I also want to return a single boolean type variable, but cant seem to get the program to do this. ... I have declared the boolean type LDMexists, so I cant see what I'm doing wrong here - how do I get it to return the five arrays and the boolean variable LDMexists? ...
    (comp.lang.java.programmer)

Quantcast