I'm trying to read consecutive bytes to a long variable.
BACKGROUND
I have code that can do this on a file. However this code is too slow for me as each file usually needs multiple reads. So I decided I could greatly speed this up by reading each file once into an array then searching the array instead. So I needed code that can do this conversion on an array instead.
And this is what I came up with:
Public 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
THE PROBLEM
But there's a catch. Before ditching the original code and replacing with my shiny new function, I decided to test a few thousand files with both methods to see whether they return the same result.
I tested over 3,000 files. And both methods returned the same result for every file tested...except three.
My new function does NOT return the correct value for these three files.
So I am looking for any forum user who has experience with reading files / converting Hex to Long. I am hoping he/she can help me work out why my function falls over on these files.
(I cant upload the files however I can provide the hex values at the exact point where the functions differ - I will do this in the next post)
Bookmarks