Is there a function that can search a byte array for a series of bytes or a string?
Printable View
Is there a function that can search a byte array for a series of bytes or a string?
Don't think so, I suppose you could turn it into a string somehow and use InStr but otherwise I would use a loop and search every so many bytes for the sequence you're looking for.
penagate,
Thanks, StrConv seems to only like text and my array can contain anything. I guess I will have to write one.
Maybe you could do this?
Although since strings are wide I'm not sure if that would work, you might have to insert zero bytes at every odd interval.VB Code:
Dim strTemp As String strTemp = Space(UBound(ByteArray)) CopyMemory ByVal strTemp, ByteArray(0), UBound(ByteArray)
Here:
Should I put this in the CodeBank ? i think I should right ?VB Code:
Option Explicit Private Sub Form_Load() Dim A(-1 To 10) As Byte, B(5 To 7) As Byte A(-1) = 100 A(0) = 5 A(1) = 6 A(2) = 67 A(3) = 3 A(4) = 34 A(5) = 76 A(6) = 12 A(7) = 34 A(8) = 2 A(9) = 1 A(10) = 99 B(5) = 12 B(6) = 34 B(7) = 2 Debug.Print SearchByteArray(A, B, -888) End Sub Public Function SearchByteArray(BigArr() As Byte, SearchArr() As Byte, Optional ByVal NotFoundRet As Long = -1) As Long Dim K As Long, Q As Long, LB As Long LB = LBound(SearchArr) For K = LBound(BigArr) To UBound(BigArr) - (UBound(SearchArr) - LB) For Q = LB To UBound(SearchArr) If BigArr(K + Q - LB) <> SearchArr(Q) Then Exit For Next Q If Q = UBound(SearchArr) + 1 Then Exit For Next K If K <= UBound(BigArr) - (UBound(SearchArr) - LB) Then SearchByteArray = K Else SearchByteArray = NotFoundRet End If End Function
By the way... when you do InStr in VB, in the background, that's how it does the search...
I thought as much, but its written in C or something, so it's a fair bit quicker than anything we could write ourselves in VB.
YUP :)Quote:
Originally Posted by penagate
I wrote the routine but it if god-awful slow. It is not an option.
Did you try CVMichael's code a few posts back?Quote:
Originally Posted by randem
Cheers,
RyanJ