searching a file for matching data 
Author Message
 searching a file for matching data

I'm having a seniors moment (63 tomorrow, that's in 13 minutes time, btw)

I'm trying to open a jpg file, find the position of "FF C0 00 01 08" and get the
next four bytes of info. (width and height of said jpg)

That data never stays in one fixed spot, so I have to find it.

Seniors moments are a pita, and I can't remember how to do it.
I could go through all those books I bought on VB6, but I'll bet you I won't
find what I want until next week, or next month.

Anyone interested in giving me a hand with the code?

(63 in 8 minutes, now)

Graham



Fri, 13 Jul 2012 21:21:19 GMT  
 searching a file for matching data
Quote:

> I'm having a seniors moment (63 tomorrow, that's in 13 minutes time, btw)

> I'm trying to open a jpg file, find the position of "FF C0 00 01 08" and
> get the next four bytes of info. (width and height of said jpg)

> That data never stays in one fixed spot, so I have to find it.

> Seniors moments are a pita, and I can't remember how to do it.
> I could go through all those books I bought on VB6, but I'll bet you I
> won't find what I want until next week, or next month.

> Anyone interested in giving me a hand with the code?

> (63 in 8 minutes, now)

> Graham

oops!

that's "FC C0 00 11 08" for those finicky {*filter*}s who probably know what I'm
supposed to be looking for.

Happy birthday to me, happy birthday too meee,  hippy burfdy to Graham...
Yep, I've been imbibing a bit as well.......

Grah .. now how do i spel that name?



Fri, 13 Jul 2012 21:29:30 GMT  
 searching a file for matching data
Quote:


>> I'm having a seniors moment (63 tomorrow, that's in 13 minutes time, btw)

>> I'm trying to open a jpg file, find the position of "FF C0 00 01 08"
>> and get the next four bytes of info. (width and height of said jpg)

>> That data never stays in one fixed spot, so I have to find it.

>> Seniors moments are a pita, and I can't remember how to do it.
>> I could go through all those books I bought on VB6, but I'll bet you I
>> won't find what I want until next week, or next month.

>> Anyone interested in giving me a hand with the code?

>> (63 in 8 minutes, now)

>> Graham

> oops!

> that's "FC C0 00 11 08" for those finicky {*filter*}s who probably know what
> I'm supposed to be looking for.

> Happy birthday to me, happy birthday too meee,  hippy burfdy to Graham...
> Yep, I've been imbibing a bit as well.......

> Grah .. now how do i spel that name?

Hellup!! Hellppp!!

The XYL is dragging me orff, I'll pick up tomorrow.



Fri, 13 Jul 2012 21:32:55 GMT  
 searching a file for matching data
Happy Birthday!

Option Explicit

Private Sub Form_Load()
    Dim b() As Byte
    Dim bFind(1 To 5) As Byte

    bFind(1) = &HFC
    bFind(2) = &HC0
    bFind(3) = &H0
    bFind(4) = &H11
    bFind(5) = &H8

    b = GetEntireFile("C:\Test.jpg")
    Debug.Print InByteArray(1, b, bFind)

End Sub

' Read the entire file into a byte array and returns it
Public Function GetEntireFile(ByRef FileName As String) As Byte()
    Dim f As Integer
    Dim FileContents() As Byte
    Dim i As Long

    f = FreeFile
    Open FileName For Binary Access Read Shared As f
    ' Allocate buffer for the entire file
    ReDim FileContents(1 To LOF(f))
    ' Read the entire file into memory
    Get f, , FileContents
    Close f
    GetEntireFile = FileContents
End Function

' Searches a binary array for a matching series of bytes. Returns the
' index of the first byte that matches the series of bytes, 0 otherwise.
Public Function InByteArray(ByVal posStart As Long, ByRef a() As Byte, _
    ByRef bFind() As Byte) As Long
    Dim i As Long
    Dim j As Long
    Dim FindLBound As Long
    Dim FindUBound As Long

    ' For performance, store LBound/UBound in variables
    ' because they don't change.
    FindLBound = LBound(bFind)
    FindUBound = UBound(bFind)

    For i = posStart To UBound(a)
        For j = FindLBound To FindUBound
            If bFind(j) <> a(i + j - FindLBound) Then
                ' Not all bytes match
                Exit For
            End If
        Next
        If j = FindUBound + 1 Then
            ' This is only True when the "j" loop completed
            ' without interruption. We can use a Boolean "Found"
            ' variable, but that uses extra variable.
            InByteArray = i
            Exit Function
        End If
    Next
End Function



Sat, 14 Jul 2012 01:33:42 GMT  
 searching a file for matching data

Quote:
> I'm having a seniors moment (63 tomorrow, that's
> in 13 minutes time, btw)

So by now you'll be heading for 64!  Still just a youngster though . . .
according to DateDiff I turned 63 about 1,805,727 minutes ago :-)

Mike



Sat, 14 Jul 2012 07:27:40 GMT  
 searching a file for matching data

Quote:

> Happy Birthday!

aww...   geeee...

<snip> 'kept for reference
ta, nobody.

That was a bit more than I was looking for.
I'm not objecting, though!!
I'd worked out most of the 'get the file part',
it was the searching that had me tossed.

I'll preface this section with your name.
(huh!! nobody?)   :->

now I'd bettter go have some brekkie - my wife just called.
I might even have one or two of those Haigh's chocolates she gave me earlier

Graham



Sat, 14 Jul 2012 07:34:47 GMT  
 searching a file for matching data
Quote:



>> I'm having a seniors moment (63 tomorrow, that's
>> in 13 minutes time, btw)

> So by now you'll be heading for 64!  Still just a youngster though . . .
> according to DateDiff I turned 63 about 1,805,727 minutes ago :-)

> Mike

God!! you ARE keeping your mind young, Mike

That's about 3 years, some months ahead of me (I used the calculator)
Should have done it with a VB program, and found out just how young you really
are. That would keep my mind working away as well

Graham



Sat, 14 Jul 2012 07:41:00 GMT  
 searching a file for matching data
Hey, old man, you have been 63 years old or older some 3.85 or so times longer than I have been.

--
Rick (MVP - Excel)

Quote:



>> I'm having a seniors moment (63 tomorrow, that's
>> in 13 minutes time, btw)

> So by now you'll be heading for 64!  Still just a youngster though . . .
> according to DateDiff I turned 63 about 1,805,727 minutes ago :-)

> Mike



Sat, 14 Jul 2012 11:30:11 GMT  
 searching a file for matching data
Quote:

> Happy Birthday!

<snip> 'saved for reference

Did some variable name changes to match the rest of my program

Worked without a hitch!!

My senior's moment was thinking about strings, not bytes

Graham



Sat, 14 Jul 2012 15:30:56 GMT  
 searching a file for matching data

Quote:
> Hey, old man, you have been 63 years old or older
> some 3.85 or so times longer than I have been.

LOL! Now you've gone and made me use DateAdd . . . and you've made me guess
whether you were using my "minutes past 63 years" at the time I sent my
message or had extrapolated it to the time you sent yours . . . and you've
made me wonder whether you were using your own age as it was at the time I
sent my message or at the time you sent yours . . . and you've made me think
about whether you are using your own (or my own) actual age from the time of
birth to the current time, or perhaps using the legal "coming of age" time
instead . . . and you've made me double check whether Windows Mail was
showing me my own local time on your message . . . and then dig out its
properties to check your own local time . .  and . . . Oh {*filter*}! . . . this
old head of mine is starting to ache now . . . were you by any chance born
at 5:30 in the morning?

<g>

Mike



Sat, 14 Jul 2012 19:07:39 GMT  
 searching a file for matching data
Quote:



>> Hey, old man, you have been 63 years old or older
>> some 3.85 or so times longer than I have been.

> LOL! Now you've gone and made me use DateAdd . . . and you've made me
> guess whether you were using my "minutes past 63 years" at the time I
> sent my message or had extrapolated it to the time you sent yours . . .
> and you've made me wonder whether you were using your own age as it was
> at the time I sent my message or at the time you sent yours . . . and
> you've made me think about whether you are using your own (or my own)
> actual age from the time of birth to the current time, or perhaps using
> the legal "coming of age" time instead . . . and you've made me double
> check whether Windows Mail was showing me my own local time on your
> message . . . and then dig out its properties to check your own local
> time . .  and . . . Oh {*filter*}! . . . this old head of mine is starting
> to ache now . . . were you by any chance born at 5:30 in the morning?

> <g>

> Mike

huh!!! I think I can follow that. just give me a minute or nine to decode the
above. Can I use VB?

Actually, I don't know what time of the day I was born. Some time on Australia
Day, 1947. Hey! I can categorically state I have never worked on my birthday.

Graham



Sat, 14 Jul 2012 21:08:05 GMT  
 searching a file for matching data


Quote:
> I'm having a seniors moment (63 tomorrow, that's in 13 minutes time, btw)

> I'm trying to open a jpg file, find the position of "FF C0 00 01 08" and get the
> next four bytes of info. (width and height of said jpg)

> That data never stays in one fixed spot, so I have to find it.

> Seniors moments are a pita, and I can't remember how to do it.
> I could go through all those books I bought on VB6, but I'll bet you I won't
> find what I want until next week, or next month.

> Anyone interested in giving me a hand with the code?

A bit late (I've been on vacation) but I thought I would add a
second method for those looking to do the same.

Quite simply, put the file data in a byte array, and the search
bytes in a byte array, then use InStrB to find their location.

InStrB will compare byte-for-byte just like InStr compares
character-for-character.  Also, those functions (InStr & InStrB)
accept byte arrays in place of strings for their parameters.

LFS



Mon, 16 Jul 2012 08:44:13 GMT  
 searching a file for matching data

Quote:
> A bit late (I've been on vacation) but I thought I would
> add a second method for those looking to do the same.
> Quite simply, put the file data in a byte array, and the
> search bytes in a byte array, then use InStrB to find
> their location. InStrB will compare byte-for-byte just
> like InStr compare character-for-character.

Well, well, well! I knew that InstrB picked out individual bytes in a
string, but I never actually used it bcause I really couldn't see the value
of it, at least for the things I usually do. But I never knew you could use
it to find one set of Byte array data inside another! Very interesting. I've
often had the need to do that and I've written byte by byte comparisons to
perform the task! I think I'm gonna have to get a timer on it and see how
much I've been missing! Now if only my old brain was still capable of
retaining new information I could leave it at that, but of course at my age
it isn't so I'll have to put it in my little database of "things I learned
since my brain filled up" ;-)  Thanks Larry.

Mike



Mon, 16 Jul 2012 09:03:45 GMT  
 searching a file for matching data
Quote:


>> I'm having a seniors moment (63 tomorrow, that's in 13 minutes time, btw)

>> I'm trying to open a jpg file, find the position of "FF C0 00 01 08" and get the
>> next four bytes of info. (width and height of said jpg)

>> That data never stays in one fixed spot, so I have to find it.

>> Seniors moments are a pita, and I can't remember how to do it.
>> I could go through all those books I bought on VB6, but I'll bet you I won't
>> find what I want until next week, or next month.

>> Anyone interested in giving me a hand with the code?

> A bit late (I've been on vacation) but I thought I would add a
> second method for those looking to do the same.

> Quite simply, put the file data in a byte array, and the search
> bytes in a byte array, then use InStrB to find their location.

> InStrB will compare byte-for-byte just like InStr compares
> character-for-character.  Also, those functions (InStr & InStrB)
> accept byte arrays in place of strings for their parameters.

> LFS

Thanks, Larry. That's what I was thinking of, originally.
I'll try modifying nobody's "bludgeon it with a large rock" code (which works
fine) to see what happens. At least I'm now getting the width and height of a
jpg file. I just wish these jpg, exif, jfif programs would all do the same thing
when they edit a jpg. (well they do, actually - it's that 'FF C0' marker that
seems to pop up anywhere from 256 bytes from the SOI, to several hundred later).

Graham



Mon, 16 Jul 2012 16:22:04 GMT  
 searching a file for matching data


Quote:

> Thanks, Larry. That's what I was thinking of, originally.
> I'll try modifying nobody's "bludgeon it with a large rock" code (which works
> fine) to see what happens. At least I'm now getting the width and height of a
> jpg file. I just wish these jpg, exif, jfif programs would all do the same thing
> when they edit a jpg. (well they do, actually - it's that 'FF C0' marker that
> seems to pop up anywhere from 256 bytes from the SOI, to several hundred later).

If you're grabbing only the header info, might you limit your buffer to 4K or 8K?
I would think that should go quicker than loading up the whole (100K +) file....

LFS



Mon, 16 Jul 2012 17:29:20 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Open a text file, search for place holder, then write data to file

2. record sections do not match data source ( data report)

3. searching items and pattern matching

4. searching listview for partial match with text

5. searching history that matches substring in url for IE

6. SQL Search with no matching results

7. Can I do dynamic partial match search like in Access (AutoExpand)

8. Search / Match hundreds of boolean fields

9. searching for a matching record in a recordset

10. Text in a textbox, Search a database, display entries that match in a listbox

11. Word searching files, with data from a cell in excel

12. RegExp question: match within another match

 

 
Powered by phpBB® Forum Software