Re: working with byte arrays
From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 11/12/04
- Next message: Savas Ates: "picture object"
- Previous message: Catsworth: "Re: Image List Problems"
- In reply to: Peter: "Re: working with byte arrays"
- Next in thread: Peter: "Re: working with byte arrays"
- Reply: Peter: "Re: working with byte arrays"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Savas Ates: "picture object"
- Previous message: Catsworth: "Re: Image List Problems"
- In reply to: Peter: "Re: working with byte arrays"
- Next in thread: Peter: "Re: working with byte arrays"
- Reply: Peter: "Re: working with byte arrays"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|