+ Reply to Thread
Results 1 to 11 of 11

Get statement - Please help me understand how this works

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Get statement - Please help me understand how this works

    I'm looking into reading file contents. I've searched the web and started using Open, Seek & Get statements in my code. Now this does what I want it to - except it is very slow for multiple reads within each file.

    So I did more research and I think it would pay if I read each file once into an array (which I will call 'buffer' from now on) and then do multiple reads within the buffer.

    The only catch is that I need an alternative to 'Get' to use on the buffer. I would write the function myself but I cant get my head around how 'Get' works.


    To give a real life example, say I have a file and the extract below gives me a lngGetHeader = 53691465.

        
        intFNum = FreeFile()
        Open strFileName For Binary Access Read Lock Write As intFNum
        lngReadPos = lngReadPos + 1
        Seek #intFNum, lngReadPos
        Get #intFNum, , lngGetHeader
    when I try to read the same data from the buffer/array instead (extract below), I get 51?!

                    
                    lngReadPos = lngReadPos + 1
                    lngGetHeader = avarBuffer(lngReadPos)

    I know I am doing something wrong but I'm not sure what. Do I need to read multiple bytes from this position to get the long variable?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    To help others to help me, I have created a test XLSM
    Attached Files Attached Files

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Get statement - Please help me understand how this works

    Using a long isn't really what you're after (or I doubt it is anyway). To tread the first Byte in a file, lngGetHeader should be a byte, if you change it to a byte then your code works as expected.

    Is the long value what you are expecting? i.e is the correct value 53691465?

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    Quote Originally Posted by Kyle123 View Post
    Using a long isn't really what you're after (or I doubt it is anyway). To tread the first Byte in a file, lngGetHeader should be a byte, if you change it to a byte then your code works as expected.

    Is the long value what you are expecting? i.e is the correct value 53691465?
    Hi Kyle, good to hear from you again.

    Good call! You are correct - In real life I am not seeking a single byte result nor am I checking the first byte in the file. (In the previous posts I tried to strip out anything that might distract and thus condense my question down to its essentials)

    So I'll explain what I am really doing. I am searching music files for a certain header. The original code achieved this by using GET to read parts of the file as a long variable (just like the code in my first post).

    So I think I that I will need a long result. However when I use a buffer, I am only reading the first byte at the Read Position. I assume that the original code is reading multiple bytes from the Read Position? But if that is the case, how come the values are so different?
    Last edited by mc84excel; 07-27-2015 at 11:03 PM.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Get statement - Please help me understand how this works

    As an example, opening a Jpeg, results in FFD8 FFE1 (which is correct - that's the magic number for a jpeg) for both methods
        Dim strFullName As String
        Dim intFNum As Integer
        Dim lngReadPos As Long, x As Long
        Dim BytGetHeader As Byte
    
        Dim avarBuffer As Variant
        Dim lngGetHeader2 As Long
    
        Dim hxValue As String
    
        'user prompted to pick file
        strFullName = fnstrFilePicker
        If Len(strFullName) > 0 Then
        Else
            MsgBox "You cancelled!"
            Exit Sub
        End If
    
        'method 1
        intFNum = FreeFile()
        Open strFullName For Binary Access Read Lock Write As intFNum
        lngReadPos = 1
        For x = 1 To 4
            Get #intFNum, x, BytGetHeader
            hxValue = hxValue & Hex(BytGetHeader)
        Next x
        Close #intFNum
    
        Debug.Print "Method 1: Header = " & hxValue
    
    
        'method 2
        hxValue = ""
        avarBuffer = fnavarUseFileOpenToCreateBufferArr(strFullName)
        For x = 0 To 3
            hxValue = hxValue & Hex(avarBuffer(x))
        Next x
        Debug.Print "Method 2: Header = " & hxValue
    Using a long in the same scenario results in -503326465, which has a hex value of FFFF FFFF E1FF D8FF
    Last edited by Kyle123; 07-27-2015 at 07:27 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    Quote Originally Posted by Kyle123 View Post
    As an example, opening a Jpeg, results in FFD8 FFE1 (which is correct - that's the magic number for a jpeg) for both methods
    Thanks Kyle. Is there anyway I can avoid the For/Next loop and the Hex values in method 2?

    If we use JPEG as example on my code in the OP, I get a long of -520103681. This in Hex is E0FFD8FF. If I open the same file in a Hex editor I get FF D8 FF E0 (reverse order of course). So I'm happy with that. I just need a way to get the result of -520103681 using method 2. If that's possible.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Get statement - Please help me understand how this works

    Ok, I still don't really get what you want. The first bit of data isn't a long?

    No, you can't use method 2 without looping, it's a byte array - why do you not want to loop?

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    Quote Originally Posted by Kyle123 View Post
    No, you can't use method 2 without looping, it's a byte array - why do you not want to loop?
    OK that well cant be helped. It's no big deal - Speed was my only concern why I wanted to minimise looping if possible.


    Quote Originally Posted by Kyle123 View Post
    Ok, I still don't really get what you want. The first bit of data isn't a long?
    I have difficulty communicating. My fault not yours. I'll try again.

    The method 1 (in my code on the OP) returns a long variable. Somehow this long represents 4 consecutive bytes in the file - exactly how this works I am not sure! (To illustrate by example - in the case of a JPEG, the first four bytes in the file have the hex values: FF D8 FF E0 ; And method 1 reads these as a long of -520103681 ?!)

    The method 2 (in your example) returns a string of hex values.

    Unfortunately I need method 2 to return a long variable - with same value as method 1. How can I do this?

    (The reason why I need method 2 to return the same long as method 1 is because: Each long variable found is tested against another sub function to see if the long variable is the information that I am looking for. This function only accepts a long argument, not a string of hex values)


    UPDATE: Solved.

    The solution was staring me in the face. :slaps forehead: I overlooked the nuisance of Little Endian. In the case of the JPG - the hex values of FF D8 FF E0 - I needed to place them in reverse order then convert to long. i.e. FF D8 FF E0 -> &HE0FFD8FF -> Clng(&HE0FFD8FF) => -520103681 = Method 1 variable of -520103681

    Thanks Kyle. I couldn't have made this connection without your help. +1
    Last edited by mc84excel; 07-28-2015 at 08:21 PM. Reason: reword for clarity

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    The thread is now solved but in case anyone with the same problem (unlikely!) stumbles across this thread looking for a quick solution, here it is:

    Replace this part of the code:
                   
    Seek #intFNum, lngReadPos
    Get  #intFNum, , lngResult
    With the extract below
    lngResult= fnlngBufferEquivalentToGETLong(avarBuffer, lngReadPos)
    I have written a custom function to replace GET for longs. See below.
    Private Function fnlngBufferEquivalentToGETLong(ByRef avarBuffer As Variant, ByVal lngReadPos As Long) As Long
    'You can use Get function on an open file to read 4 bytes to a long variable from a Seek position
    'This function is designed as an equivalent for when you use a Buffer arr instead
        Dim strHexValue As String
        strHexValue = "&H" & Hex(avarBuffer(lngReadPos + 3)) & Hex(avarBuffer(lngReadPos + 2)) & Hex(avarBuffer(lngReadPos + 1)) & Hex(avarBuffer(lngReadPos))
        fnlngBufferEquivalentToGETLong = CLng(strHexValue)
    End Function
    You have permission to do what you want with the above function (yes, including commercial use) however: a) I would appreciate it if you didn't pass this off as your own work and b) blanket disclaimer - I offer no guarantee that it will work. You use it entirely at your risk. I accept no liability whatsoever and I do not take any responsibility for any damage/s - real or imagined - that you may incur etc.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Get statement - Please help me understand how this works

    As to your other question, get returns 4 bytes for a long since that's the size of a long. It's the reason you use fixed length strings with get, so it knows how many bytes to return - the size of the data type, that was my original point of changing long to byte, it lets you get one at a time - though I fear I didn't explain this very well.

    Glad you got it sorted.

    P.s looping isn't slow, it gets a bad rep since looping through a worksheet is. The bad part isn't the loop, it's the repeated calls to the worksheet object, each of which is very slow, that's why you read and write ranges in bulk and loop the intermediary variant array

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Get statement - Please help me understand how this works

    Something may be wrong with my alternative GET function.

    Before abandoning the previous method, I ran the full code (original version) on over 3000 files. At the same time, I also ran the full code (new version). And compared the end results for each file. The end results were the same for both methods on all but 3 files. But I want 0% margin of error for this project.

    Have I made a wrong assumption somewhere on my fnlngBufferEquivalentToGETLong?


    nvm - I have started a new thread for this. http://www.excelforum.com/excel-prog...ary-level.html
    Last edited by mc84excel; 07-30-2015 at 07:53 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. IF AND statement that works across a huge number of rows
    By kcphila in forum Excel Formulas & Functions
    Replies: 27
    Last Post: 07-10-2015, 01:09 PM
  2. [SOLVED] I want to understand what is behind A2 cell ? How this works ?
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2014, 01:36 PM
  3. IF statement works for some but not all?
    By refryguy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-02-2014, 02:04 PM
  4. [SOLVED] If statement that only returns #value, however it works in another spreadsheet but has {}
    By michael_bemis2002 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-01-2012, 03:56 PM
  5. Replies: 1
    Last Post: 09-18-2007, 02:07 PM
  6. [SOLVED] SUMPRODUCT statement only works sometimes
    By Richard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2006, 12:55 PM
  7. Select case statement - it will not work and i dont understand why! :o(
    By Thomas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-26-2005, 01:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1